<?php
//get shop page's title
$t = $site->pageTitle($s['shop_page']);
$seo_pageTitle = seo_str($t);
//check and apply discount code if it is valid
if(isset($_POST['code']))
{
$code = $form->clean_fields($_POST['code']);
$query = $mysql->query("ID", "fs_shop_discountcodes", "code = '$code' AND exp_stamp >= ".time()." LIMIT 1");
if($mysql->num_rows($query) == 0)
{
$form->error[] = 'Sorry, discount/voucher code you provided is invalid.';
$error_msg = $form->show_error();
}
else
{
$mysql->update("discount_code", $code, "fs_shop_orderdata", "owner = '{$_SESSION['owner']}' AND temp = 'y' LIMIT 1");
}
}
//remove temp orders and items from the cart
$aMonth = strtotime("-35 days");
$mysql->Delete("fs_shop_cart", "temp = 'y' AND stamp <= $aMonth");
$mysql->Delete("fs_shop_orderdata", "temp = 'y' AND shipped = 'n' AND stamp <= $aMonth");
//remove item from basket
if($_GET['junk2'] == 'Remove')
{
$remove = (int)$_GET['junk3'];
$mysql->Delete("fs_shop_cart", "owner = '{$_SESSION['owner']}' AND ID = $remove AND temp = 'y' LIMIT 1");
header("Location: /{$s['shop_page']}/$seo_pageTitle/Basket");
exit;
}
//redirect to checkout
if(isset($_POST['checkout']))
{
header("Location: /{$s['shop_page']}/$seo_pageTitle/Checkout");
exit;
}
//update qty
if(isset($_POST['update_qty']))
{
$qtys = $form->clean_fields($_POST['qty']);
foreach($qtys as $line => $qty)
{
//get stock
list($id, $option) = explode("|", $line);
$id = (int)$id;
$option = addslashes($option);
$inStock = $mysql->get_array($mysql->query("fs_shop_stock.stock,fs_shop_stock.pid", "fs_shop_cart,fs_shop_stock", "fs_shop_cart.owner = '{$_SESSION['owner']}' AND fs_shop_cart.temp = 'y' AND fs_shop_cart.ID = $id AND fs_shop_stock.option = '$option' AND fs_shop_stock.pid = fs_shop_cart.pid LIMIT 1"));
$mysql->update("qty", $qty, "fs_shop_cart", "owner = '{$_SESSION['owner']}' AND temp = 'y' AND ID = $id LIMIT 1");
}
$error_msg = $form->show_error();
$mysql->Delete("fs_shop_cart", "temp = 'y' AND qty <= 0");
}
//show "already in basket error if session is set
if($_SESSION['alreadyInCart'] == 'yes')
{
$_SESSION['alreadyInCart'] = '';
$error_msg = '<p style="font-weight:bold;">You already have this product in your cart, please change the QTY to order more of the same product.</p>';
}
//stock checks
$outOfStockMsg = '';
$notEnoughStock = '';
$query = $mysql->query("item_name,pid,`option`,qty", "fs_shop_cart", "owner = '{$_SESSION['owner']}' AND temp = 'y' ORDER BY ID");
while($data = $mysql->get_array($query))
{
$st = $mysql->get_array($mysql->query("stock", "fs_shop_stock", "pid = {$data['pid']} AND `option` = '".addslashes($data['option'])."' LIMIT 1"));
if($st['stock'] <= 0)
{
$outOfStockMsg .= stripslashes('<strong>'.$data['item_name'].'('.$data['option'].')</strong> is out of stock.<br />');
}
if($data['qty'] > $st['stock'] && $st['stock'] > 0)
{
$notEnoughStock .= 'We only have <strong>'.$st['stock'].'</strong> of <strong>'.stripslashes($data['item_name'].'('.$data['option'].')</strong> in stock.<br />');
}
}
//show out of stock message
if(!empty($outOfStockMsg))
{
$error_msg .= '<div class="error_message"><div>While you were adding items to the basket and looking around our web site, some of the items you have in your basket had their stock status updated.<br /><br />'.$outOfStockMsg.'<br />You will not be able to complete checking out until you adjust the QTY of issue items below. We apologise for any inconvenience this may cause.</div></div>';
}
//show less in stock than wanted message
if(!empty($notEnoughStock))
{
$error_msg .= '<div class="error_message"><div>While you were adding items to the basket and looking around our web site, some of the items you had in your basket had their stock status updated.<br /><br />'.$notEnoughStock.'<br />You will not be able to complete checking out until you just the adjust the QTY of issue items below. We apologise for any inconvenience this may cause.</div></div>';
}
//show basket
include(fileInclude('inc/shop/discount/index.php'));//check and update discount
$mainhtml .= '<h1>Basket</h1><p id="shopCrumbs"><a href="/'.$s['shop_page'].'/'.$seo_pageTitle.'">'.$t.'</a> &gt; <strong>Basket</strong></p>'.$error_msg;
$query = $mysql->query("ID,pid,`option`,(price*qty) as price,qty,custom_field_val,extra_info", "fs_shop_cart", "owner = '{$_SESSION['owner']}' AND temp = 'y' ORDER BY ID ASC");
$items_in_cart = $mysql->num_rows($query);
if($items_in_cart > 0)
{
$t = $site->shop_settings('basketpage_msg');
$basketpage_msg = (!empty($t['basketpage_msg'])) ? '<p>'.nl2br($t['basketpage_msg']).'</p>' : '';
$mainhtml .= $basketpage_msg.'<form id="basket" name="basket" method="post" action=""><table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><th colspan="2" align="left">Description</th><th width="100" style="text-align: center;">QTY</th><th width="100" style="text-align: center;">Total</th></tr>';
}
else
{
$mainhtml .= '<p>You haven\'t added any products to your basket yet. <a href="/'.$s['shop_page'].'/'.$seo_pageTitle.'">Start shopping!</a></p>';
}
$total = 0;
while($data = $mysql->get_array($query))
{
$product = $site->productInfo($data['pid']);
$data['custom_field_val'] = (!empty($data['custom_field_val'])) ? '<br>'.$product['custom_field_lbl'].': '.$data['custom_field_val'] : '';
$prdlink = ($product['cat'] == 0) ? '#' : '/'.$s['shop_page'].'/'.$seo_pageTitle.'/'.$product['url_key'];
$data['extra_info'] = ($data['extra_info'] != '') ? '<br />'.stripslashes($data['extra_info']) : '';
$mainhtml .= '<tr><td width="80"><a href="'.$prdlink.'"><img src="/inc/shop/images/clear.gif" alt="'.$product['name'].'" border="0" style="background-image: url(/inc/shop/images/'.$product['tiny_image'].');" /></a></td><td><a href="'.$prdlink.'">'.$product['name'].'</a><br /><small>'.$data['option'].$data['custom_field_val'].$data['extra_info'].'</small></td>
<td align="center"><input name="qty['.$data['ID'].'|'.$data['option'].']" type="text" id="qty['.$data['ID'].'|'.$data['option'].']" value="'.$data['qty'].'" size="3" maxlength="3" /><br /><small><a href="/'.$s['shop_page'].'/'.$seo_pageTitle.'/Basket/Remove/'.$data['ID'].'" title="Remove this product from your basket">Remove</a></small></td>
<td align="center">'.$site->currency_symbol($data['price']).'</td></tr>';
$total += $data['price'];
}
//cart total and discount
if($items_in_cart > 0)
{
$order = $mysql->get_array($mysql->query("discount_value", "fs_shop_orderdata", "owner = '{$_SESSION['owner']}' AND temp = 'y' LIMIT 1"));
$discountValue = $order['discount_value'];
if($discountValue > 0)
{
$mainhtml .= '<tr>
<td align="right" colspan="3"><strong style="margin-right:10px;">Discount</strong></td>
<td nowrap="nowrap"><strong>'.$site->currency_symbol($discountValue).'</strong></td>
</tr>';
}
$mainhtml .= '<tr>
<td align="right" colspan="3"><strong style="margin-right:10px;">Pre-shipping Total</strong></td>
<td nowrap="nowrap"><strong>'.$site->currency_symbol($total-$discountValue).'</strong></td>
</tr>';
//is shop is using flat rate shipping, show shipping and total rows
$type = $site->shop_settings('shipping_method,payNowPickLater_payment_method');
if($type['shipping_method'] == 'flatrate')
{
include(fileInclude('inc/shop/shipping/flatrate.php'));
}
if($type['shipping_method'] == 'custom')
{
include(fileInclude('inc/shop/shipping/custom.php'));
}
$shippingFlat = $mysql->get_array($mysql->query("shipping", "fs_shop_orderdata", "owner = '{$_SESSION['owner']}' AND temp = 'y' LIMIT 1"));
if($type['payNowPickLater_payment_method'] == 'n' && $shippingFlat['shipping'] > 0)
{
$mainhtml .= '<tr id="cartShipping">
<td align="right" colspan="3"><strong style="margin-right:10px;">Shipping</strong></td>
<td nowrap="nowrap"><strong>'.$site->currency_symbol($shippingFlat['shipping']).'</strong></td>
</tr>
<tr id="cartTotal">
<td align="right" colspan="3"><strong style="margin-right:10px;">Total</strong></td>
<td nowrap="nowrap"><strong>'.$site->currency_symbol($total-$discountValue+$shippingFlat['shipping']).'</strong></td>
</tr>';
}
//cont shopping button
$lastAdded = $mysql->get_array($mysql->query("pid", "fs_shop_cart", "owner = '{$_SESSION['owner']}' AND temp = 'y' ORDER BY ID DESC LIMIT 1"));
$product = $site->productInfo($lastAdded['pid']);
$product['cat'] = (int)$product['cat'];
$lastAddedCat = $mysql->get_array($mysql->query("url_key", "fs_shop_cats", "ID = {$product['cat']} LIMIT 1"));
if(!empty($lastAddedCat['url_key']))
{
$contShopping = '<input type="button" name="contShop" id="contShop" value="Continue Shopping" class="button" style="margin-right:20px;" onclick="window.location=\'/'.$s['shop_page'].'/'.$seo_pageTitle.'/'.$lastAddedCat['url_key'].'\';" />';
}
else
{
$contShopping = '<input type="button" name="contShop" id="contShop" value="Continue Shopping" class="button" style="margin-right:20px;" onclick="window.location=\'/'.$s['shop_page'].'/'.$seo_pageTitle.'\';" />';
}
//end of table
$mainhtml .= '</table>

<p>'.$contShopping.'<input type="submit" name="update_qty" id="update_qty" value="Update QTY" style="margin-right:20px;" class="button" /><input type="submit" name="checkout" id="checkout" value="Checkout &gt;&gt;" class="button" /></p></form>

<form name="discountCode" id="discountCode" method="post" action="" style="margin-top:30px;"><p>Gift voucher or promo code: <input type="text" name="code" id="code" size="32" /> <input type="submit" name="button" id="button" value="Apply"></p></form>';
}
//include this add-ons's overwrite css file from the template being used, if there is one
$t = settings('template');
if(file_exists('templates/'.$t['template'].'/addons/shop.css'))
{
$mainhtml = str_replace("</head>", '<link rel="stylesheet" type="text/css" href="/templates/'.$t['template'].'/addons/shop.css" /></head>', $mainhtml);
}
else
{
$mainhtml = str_replace("</head>", '<link rel="stylesheet" type="text/css" href="//www.freesites.com.au/addons/shop.css" /></head>', $mainhtml);
}
//end
?>