<?php
$domain = $data['domain'];
//don't charge shipping if order only has virutal products
$virtualOnly = false;
$allitems = $mysql->num_rows($mysql->query("ID", "fs_shop_cart", "owner = '{$_SESSION['owner']}' AND temp = 'y'"));
$virtualItems = $mysql->num_rows($mysql->query("ID", "fs_shop_cart", "owner = '{$_SESSION['owner']}' AND temp = 'y' AND `virtual` = 'y'"));
if($virtualItems == $allitems)
{
$virtualOnly = true;
}
//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;
}
$weight += ($loop == true) ? 200 : 0;//add 200g for packaging weight
$postcode = $site->shop_settings('my_postcode');
$order = $mysql->get_array($mysql->query("postcode,town", "fs_shop_orderdata", "owner = '{$_SESSION['owner']}' AND temp = 'y' LIMIT 1"));
//Build URL and Querystring parameters to lookup postage cost
$sURL = "https://www.freesites.com.au/fastway/?from={$postcode['my_postcode']}&to=".str_replace(' ', '', $order['postcode'])."&weight=".($weight/1000).'&suburb='.urlencode($order['town']).'&site='.urlencode($_SERVER['SERVER_NAME']);
$contents = @file($sURL);
$postage = trim($contents[0]);
$postage = number_format($postage, 2, '.', '');
//error checking
if($contents[0] == 'error' && $virtualOnly == false)
{
header("Location: /{$s['shop_page']}/$seo_pageTitle/Shipping_Error/");
exit;
}
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
if($virtualOnly == true)
{
$mysql->update("shipping", 0, "fs_shop_orderdata", "owner = '{$_SESSION['owner']}' AND temp = 'y' LIMIT 1");
$virtualOnly = true;
}
?>