<?php
//work out cart weight
$loop = false;
$query = $mysql->query("pid,`option`,qty", "fs_shop_cart", "owner = '{$_SESSION['owner']}' AND temp = 'y'");
while ($data = $mysql->get_array($query))
{
    $item = $site->productInfo($data['pid']);
    $weight += ($site->totalProductWeight($data['pid'], $data['option']) * $data['qty']);
    $loop = ($item['weight'] > 0) ? true : $loop;
}
$postcode = $site->shop_settings('my_postcode');
$order = $mysql->get_array($mysql->query("postcode,country,notes", "fs_shop_orderdata", "owner = '{$_SESSION['owner']}' AND temp = 'y' LIMIT 1"));
//Build URL and Querystring parameters to lookup postage cost
$sURL = 'https://www.cloudtools.com.au/postOffice/?api_key=38a32d6b40dd8ce7c0c5d23594d8897d';
$sURL .= '&country_code='.$order['country'];
$sURL .= '&from_postcode='.$postcode['my_postcode'];
$sURL .= '&to_postcode='.str_replace(' ', '', $order['postcode']);
$sURL .= '&weight='.ceil($weight / 1000);
$dom = ($order['notes'] == 'Express') ? 'AUS_PARCEL_EXPRESS' : 'AUS_PARCEL_REGULAR';
$sURL .= ($order['country'] == 'AU') ? '&code='.$dom : '&code=INTL_SERVICE_AIR_MAIL';
$sURL .= '&shippingOnly=y';
$postage = file($sURL);
$postage = number_format($postage[0], 2, '.', '');
//error checking
if ($postage <= 0 && $loop == true)
{
    header("Location: /{$s['shop_page']}/$seo_pageTitle/Shipping_Error/");
    exit;
}
//add handling fee into the shipping fee
$h = $site->shop_settings('shipping_handling_fee');
$postage += $h['shipping_handling_fee'];
//update postage
$mysql->update("shipping", $postage, "fs_shop_orderdata", "owner = '{$_SESSION['owner']}' AND temp = 'y' LIMIT 1");
//free shipping when X amount is spent
$totalvar = 0;
$query = $mysql->query("(price*qty) as price,pid", "fs_shop_cart", "owner = '{$_SESSION['owner']}' AND temp = 'y'");
while ($data = $mysql->get_array($query))
{
    $exclude = $mysql->num_rows($mysql->query("ID", "fs_shop_products", "ID = '{$data['pid']}' AND exclude_from_free_shipping = 'y' LIMIT 1"));
    $totalvar += ($exclude == 1) ? 0 : $data['price'];
}
$order = $mysql->get_array($mysql->query("sales_tax,country,discount_value", "fs_shop_orderdata", "owner = '{$_SESSION['owner']}' AND temp = 'y' LIMIT 1"));
$totalvar -= ($order['sales_tax'] + $order['discount_value']);
if ($totalvar > 0)
{
    $freeAmount = $site->shop_settings('free_shipping,home_country');
    if ($totalvar > $freeAmount['free_shipping'] && $order['country'] == $freeAmount['home_country'])
    {
        $mysql->update("shipping", 0, "fs_shop_orderdata", "owner = '{$_SESSION['owner']}' AND temp = 'y' LIMIT 1");
    }
}
//don't charge shipping if order only has virutal products
$query = $mysql->query("virtual", "fs_shop_cart", "owner = '{$_SESSION['owner']}' AND temp = 'y'");
$allitems = $mysql->num_rows($query);
$virtualItems = 0;
while ($data = $mysql->get_array($query))
{
    if ($data['virtual'] == 'y')
    {
        $virtualItems++;
    }
}
if ($virtualItems == $allitems)
{
    $mysql->update("shipping", 0, "fs_shop_orderdata", "owner = '{$_SESSION['owner']}' AND temp = 'y' LIMIT 1");
}
