<?php
$order = $mysql->get_array($mysql->query("country", "fs_shop_orderdata", "owner = '{$_SESSION['owner']}' AND temp = 'y' LIMIT 1"));
$shipping = $site->shop_settings('home_country');
if($order['country'] == '')
{
$order['country'] = $shipping['home_country'];
}
//work out shipping cost per item
$sort = ($order['country'] == $shipping['home_country']) ? 'IF(fs_shop_products.shipping_dom > fs_shop_products.shipping_with_another_item, fs_shop_products.shipping_dom, fs_shop_products.shipping_with_another_item)' : 'IF(fs_shop_products.shipping_int > fs_shop_products.shipping_with_another_item_intl, fs_shop_products.shipping_int, fs_shop_products.shipping_with_another_item_intl)';
$postage = 0;
$y = 0;
$query = $mysql->query("fs_shop_cart.pid,fs_shop_cart.qty", "fs_shop_cart,fs_shop_products", "fs_shop_products.ID = fs_shop_cart.pid AND fs_shop_cart.owner = '{$_SESSION['owner']}' AND fs_shop_cart.temp = 'y' ORDER BY $sort DESC");
while($data = $mysql->get_array($query))
{
$item = $site->productInfo($data['pid']);
if($item['ship_with_another_item'] == 'n')
{
$int_cost = ($item['shipping_int'] * $data['qty']);
$dom_cost = ($item['shipping_dom'] * $data['qty']);
}
///
if($item['ship_with_another_item'] == 'y' && $y == 0)
{
$dom_cost = $item['shipping_dom'] + ($item['shipping_with_another_item'] * ($data['qty']-1));
}
if($item['ship_with_another_item'] == 'y' && $y > 0)
{
$dom_cost = $item['shipping_with_another_item'] + ($item['shipping_with_another_item'] * ($data['qty']-1));
}
///
if($item['ship_with_another_item'] == 'y' && $y == 0)
{
$int_cost = $item['shipping_int'] + ($item['shipping_with_another_item_intl'] * ($data['qty']-1));
}
if($item['ship_with_another_item'] == 'y' && $y > 0)
{
$int_cost = $item['shipping_with_another_item_intl'] + ($item['shipping_with_another_item_intl'] * ($data['qty']-1));
}
///
$postage += ($order['country'] == $shipping['home_country']) ? $dom_cost : $int_cost;
$y++;
}
//add handling fee into the shipping fee
$h = $site->shop_settings('shipping_handling_fee');
$postage += $h['shipping_handling_fee'];
//update postage
$mysql->update("shipping", number_format($postage, 2, '.', ''), "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.00", "fs_shop_orderdata", "owner = '{$_SESSION['owner']}' AND temp = 'y' LIMIT 1");
}
}
?>