get(); if($_POST['destCountry'] AND $_POST['destCountry'] != $cartValuesArrTmp['destCountry']) $changeCountry = true; if($changeCountry) { unset($cartValuesArrTmp['destCity'], $cartValuesArrTmp['destState'], $cartValuesArrTmp['destPostalCode'], $_POST['destPostalCode'], $_POST['destCity'], $_POST['destState']); } if($_POST['selectedShippingMethod']) $shippingCourier = substr($_POST['selectedShippingMethod'], 0, strpos($_POST['selectedShippingMethod'], '_')); $cartVals->set(array( 'selectedAddressId'=>($_POST['selectedAddressId']? $_POST['selectedAddressId']:$cartValuesArrTmp['selectedAddressId']), 'destState'=>($_POST['destState']? $_POST['destState']:$cartValuesArrTmp['destState']), 'destCity'=>($_POST['destCity']? $_POST['destCity']:$cartValuesArrTmp['destCity']), 'destCountry'=>($_POST['destCountry']? $_POST['destCountry']:$cartValuesArrTmp['destCountry']), 'destPostalCode'=>($_POST['destPostalCode']? $_POST['destPostalCode']:$cartValuesArrTmp['destPostalCode']), 'customerHasAccount'=>$_GET['customerHasAccount'], 'customerAskedAboutAccount'=>$_GET['customerAskedAboutAccount'], 'shipping_method'=>($_POST['selectedShippingMethod']? $_POST['selectedShippingMethod']:$cartValuesArrTmp['shipping_method']), 'shipping_currier'=>$shippingCourier)); $cartValuesArr = $cartVals->get(); if(is_array($cartValuesArr)) extract($cartValuesArr); if(!$_POST['selectedShippingMethod'] AND $shipping_method) $selectedShippingMethod = $shipping_method; else $selectedShippingMethod = $_POST['selectedShippingMethod']; $destPostalCode = $cart->cleanPostalCode($destPostalCode); $action = ($_GET['action'])? $_GET['action']:$_POST['action']; if($_POST['Checkout_x']) $action = 'checkout'; $cart->setRefererUrl(getenv("HTTP_REFERER")); $refererUrl = $cart->getRefererUrl(); switch($action) # Main decision making { case "add_item": $_POST['qty'] = ($_POST['qty'])? $_POST['qty']:1; $cart->AddItem(cleaner::intOnly($_POST['id']), cleaner::intOnly($_POST['qty']), $_POST['size'], $_POST['attributes']); break; case "update_cart": $cart->UpdateCart(); if($_GET['coupon']) { $couponInfo = $cartCoupons->checkCoupon($_GET['coupon']); if($couponInfo['id']) $cartVals->coupon_id = $couponInfo['id']; } break; case "remove_item": $cart->RemoveItem($_GET['id']); break; case "checkout": $subTotal = $cart->getSubTotal(); $cartVals->set(array('cart_shipping'=>($cartValuesArr['cart_shipping'] - $cartValuesArr['shipping_discount']))); $zipArr = $cart->zipcodeLookup($destPostalCode,$destCountry); $destState = $zipArr['state']; $destCity = ($destCitySaved? $destCitySaved:$zipArr['city'][0]); $salesTaxAmount = $tax->get($destState, $destCity, $subTotal); echo 'checkout'; if(SITE_SSL) $secureSiteUrl = str_replace('http:','https:',$settings->site_url); else $secureSiteUrl = $settings->site_url; header("Location: ".$secureSiteUrl."/checkout.html"); exit; break; } $subTotal = $cartTotal = $cart->getSubTotal(); $products = $cart->getItems(); $cartVals->cart_subtotal = $subTotal; ####### coupon system function remove_item_by_value($array, $val = '', $preserve_keys = true) { if (empty($array) || !is_array($array)) return false; if (!in_array($val, $array)) return $array; foreach($array as $key => $value) { if ($value == $val) unset($array[$key]); } return ($preserve_keys === true) ? $array : array_values($array); } if($coupon) { $couponInfo = $cartCoupons->getWithCode($coupon); $currentCouponId[] = $couponInfo['id']; } if(is_array($products)) foreach($products as $prod) { $previousCouponsList = explode('|',$prod['coupon_ids']); if($previousCouponsList[0] AND $currentCouponId[0]) $couponIdList = array_merge($currentCouponId, $previousCouponsList); elseif($previousCouponsList[0]) $couponIdList = $previousCouponsList; elseif($currentCouponId[0]) $couponIdList = $currentCouponId; if(is_array($couponIdList)) $couponIdList = array_unique($couponIdList); #echo ''; print_r($prod); echo ' couponIdList:'; print_r($couponIdList); echo ' previousCouponsList:'; print_r($previousCouponsList); echo ''; if(is_array($couponIdList)) foreach($couponIdList as $couponId) { if(!$couponId) continue; $couponInfo = $cartCoupons->get($couponId); # run basic checks on coupon to make sure it is valid if($cartCoupons->runChecks($couponInfo)) { if($cartCoupons->checkProduct($prod['product_id'])) { # get the discount amount for this product switch($couponInfo['type']) { case 'Percentage': $couponDiscount = $prod['price'] * (($couponInfo['discount'])/100); break; case 'Fixed Amount': $couponDiscount = (($prod['price'] - $couponInfo['discount']) < 0? 0:$couponInfo['discount']); break; } #echo ''; print_r($couponInfo); echo 'discount:'.$couponDiscount.''; # add up the coupon discounts for this product and if "applies_per_item" is checked on the coupon editor than multiply the discount by the qty of the product in the cart. $productDiscount += $couponDiscount * ($couponInfo['applies_per_item']? $prod['qty']:1); unset($couponDiscount); $allProducts = true; } else { $couponIdList = remove_item_by_value($couponIdList, $couponId); } } else { trigger_error("The coupon ".$couponInfo['code']." is no longer valid."); } } $finalDiscount += $productDiscount; if(count($couponIdList)) $prodCouponIdsStr = implode('|',$couponIdList); $cart->changeProduct(array('product_id'=>$prod['product_id'], 'discount'=>$productDiscount, 'coupon_ids'=>$prodCouponIdsStr)); unset($productDiscount,$couponIdList,$prodCouponIdsStr,$previousCouponsList); } $cartVals->cart_discount = $finalDiscount; if(!$allProducts AND $couponId) trigger_error("There are no products in your cart that this coupon applies to.",512); # end coupon system # check if current products in the cart have coupons for them. if($settings->truecart_coupons) { $productCouponList = $CouponProducts->getProdList(); $categoryCouponList = $CouponCategories->getCateList(); foreach($categoryCouponList as $cateId) { $productCouponList = array_merge($productCouponList, $ProductsInCategory->getProdList()); } foreach($products as $prod) { if(in_array($prod['product_id'],$productCouponList)) $couponProductAvailable = true; } } $allItemsShipFree = $cart->allItemsShipFree(); if($customerLoggedIn) # Customer is logged in { $customer->setId($login->id()); if($cart->shippingNeeded()) # do any products need shipping? { if(is_array($editAddress)) { $country = $editAddress['country']; $state = $editAddress['state']; } if($cartVals->shippingAddressId) # have they selected a shipping address? { $destCountrySel = new formInput(array('type'=>'select', 'name'=>'destCountry', 'label'=>'Select country you\'re shipping to.', 'onchange'=>'document.cartForm.submit()', 'selected'=>$destCountry),$cart->makeKeyValueArray($settings->cart_shipping_country_list)); $destCountrySel->addFirstOption('Select Country...'); $shipAddress = $adder->getSingle($cartVals->shippingAddressId); $salesTaxAmount = $tax->get($shipAddress['state'], $shipAddress['city'], $subTotal); $shipAddress['zip'] = $shipAddress['postal_code']; $shippingArr = $shipping->get($subTotal, $shipAddress); # get shipping quote $shippingMethods = $shippingArr['rate']; $shippingRateError = $shippingArr['error']; $shippingFlatRate = $shippingArr['flat_rate']; $shippingFees = $shippingArr['fees']; $shippingDiscounts = $shippingArr['discounts']; #$shippingFees = $shippingArr['fees']; /*if(is_array($shippingMethods)) { if($selectedShippingMethod) $shippingAmount = $shippingMethods[$selectedShippingMethod];} else $shippingAmount = $shippingMethods;*/ if(is_array($shippingMethods)) { if($selectedShippingMethod) { $shippingAmount = $shippingMethods[$selectedShippingMethod]; if(is_array($shippingDiscounts)) $shippingDiscount = $shippingDiscounts[$selectedShippingMethod]; } } else $shippingAmount = $shippingMethods; $cartVals->set(array('shipping_discount'=>$shippingDiscount)); $cartTotal = $subTotal + $salesTaxAmount + $shippingAmount - $shippingDiscount; $destCountry = $shipAddress['country']; $destState = $shipAddress['state']; $cartVals->set(array('destCity'=>$shipAddress['city'], 'destState'=>$shipAddress['state'], 'destPostalCode'=>$shipAddress['postal_code'], 'destCountry'=>$shipAddress['country'], 'shipping_method'=>$selectedShippingMethod, 'shipping_currier'=>$shipping->getCourier($selectedShippingMethod), 'cart_total'=>$cartTotal, 'cart_shipping'=>$shippingAmount, 'cart_subtotal'=>$subTotal, 'cart_salesTax'=>$salesTaxAmount)); if(!$allItemsShipFree) $render[] = 'tpl_shipping_new_address_boxes'; } else { $customerAddresses = $customer->getAddresses(); if(count($newAddrFormObj)) // invalid form { $html .= "
"; foreach($newAddrFormObj as $key => $value) { if(!$value[result]) $html .= "
$value[error]
"; if(!$value[result]) $err[$key] = "fieldError"; $val[$key] = urldecode($value[value]); } if($error) $html .= "
$error
"; $html .= "
"; } $newCountrySel = new formSelect(array('name'=>'country', 'label'=>'Country', 'selected'=>$country), $cart->makeKeyValueArray($settings->cart_shipping_country_list)); $newStateSel = new formSelect(array('name'=>'state', 'first'=>'Select State...', 'selected'=>$state), getData('all_states')); if(count($newAddrFormObj)) // invalid form { $html .= "
"; foreach($newAddrFormObj as $key => $value) { if(!$value[result]) $html .= "
$value[error]
"; if(!$value[result]) $err[$key] = "fieldError"; $val[$key] = urldecode($value['value']); } if($error) $html .= "
$error
"; $html .= "
"; } if(!$allItemsShipFree) $render[] = 'tpl_shipping_new_address_boxes'; } } if(!$adder->valid())// invalid form { if(is_array($newAddrFormObj)) { $html .= "
"; foreach($newAddrFormObj as $key => $value) { if(!$value[result]) $html .= "
$value[error]
"; if(!$value[result]) $err[$key] = "fieldError"; $val[$key] = urldecode($value[value]); } if($error) $html .= "
$error
"; $html .= "
"; } if(is_array($editAddrFormObj)) { $html .= "
"; foreach($editAddrFormObj as $key => $value) { if(!$value[result]) $html .= "
$value[error]
"; if(!$value[result]) $err[$key] = "fieldError"; $val[$key] = urldecode($value[value]); } if($error) $html .= "
$error
"; $html .= "
"; } } } else # Not logged in { if($cart->shippingNeeded()) # do any products need shipping? { $salesTaxAmount = $shippingAmount = 0; if($destCountry=="US") { $zipArr = $cart->zipcodeLookup($destPostalCode, $destCountry); $destCity = ($destCitySelected)? $destCitySelected:((count($zipArr['city']) == 1)? $zipArr['city'][0]:null); $destCityArr = (count($zipArr['city'])>1)? $zipArr['city']:null; $destState = $zipArr['state']; if($zipArr['error']) trigger_error($zipArr['message'],512); } if($settings->cart_shipping_us_only) { $shipping->get($subTotal, array('city'=>$destCity, 'state'=>$destState, 'zip'=>$destPostalCode, 'country'=>$destCountry)); } else { # get sales tax if($destCountry == 'US' AND $destState) $salesTaxAmount = $tax->get($destState, $destCity, $subTotal); # if location is submitted, get shipping quote AND $destState AND $destPostalCode if($destCountry AND $destCity) { $shippingArr = $shipping->get($subTotal, array('city'=>$destCity, 'state'=>$destState, 'zip'=>$destPostalCode, 'country'=>$destCountry)); $shippingMethods = $shippingArr['rate']; $shippingRateError = $shippingArr['error']; $shippingFlatRate = $shippingArr['flat_rate']; $shippingFees = $shippingArr['fees']; $shippingDiscounts = $shippingArr['discounts']; #echo $shippingFees; # auto select if only one if(!$selectedShippingMethod) { if(count($shippingMethods) == 1) { $shipMethodTypes = array_keys($shippingMethods); $selectedShippingMethod = $shipMethodTypes[0]; } else { $shipMethodTypes = array_keys($shippingMethods); $selectedShippingMethod = $shipMethodTypes[0]; } $cartVals->set(array('shipping_method'=>$selectedShippingMethod, 'shipping_currier'=>$shipping->getCourier($selectedShippingMethod))); } if(is_array($shippingMethods)) { if($selectedShippingMethod) { $shippingAmount = $shippingMethods[$selectedShippingMethod]; if(is_array($shippingDiscounts)) $shippingDiscount = $shippingDiscounts[$selectedShippingMethod]; } } else $shippingAmount = $shippingMethods; $cartVals->set(array('shipping_discount'=>$shippingDiscount)); $cartTotal = $subTotal + $salesTaxAmount + $shippingAmount - $shippingDiscount; } # make country select menu $destCountrySel = new formInput(array('type'=>'select', 'name'=>'destCountry', 'label'=>'Select country you\'re shipping to.', 'onchange'=>'document.cartForm.submit()', 'selected'=>$destCountry),$cart->makeKeyValueArray($settings->cart_shipping_country_list)); $destCountrySel->addFirstOption('Select Country...'); # make US cities select menu if more than one city returned. if(is_array($destCityArr)) $destCitySel = new formInput(array('name'=>'destCitySelected', 'onchange'=>'document.cartForm.submit()', 'selected'=>($destCitySelected? $destCitySelected:$destCity)),$destCityArr); if($destCountry) { $cartVals->set(array('destState'=>$destState, 'destCity'=>$destCity, 'destCountry'=>$destCountry, 'destPostalCode'=>$destPostalCode, 'shipping_currier'=>$shipping->getCourier($selectedShippingMethod), 'shipping_method'=>$selectedShippingMethod, 'cart_total'=>$cartTotal, 'cart_shipping'=>$shippingAmount, 'cart_subtotal'=>$subTotal, 'cart_salesTax'=>$salesTaxAmount)); } } #if(!$allItemsShipFree) $render[] = 'tpl_shipping_cost_form'; } } # update the cart total $cartTotal = $cartVals->cart_total = $subTotal + $salesTaxAmount + $shippingAmount - $cartVals->cart_discount - $shippingDiscount; if(is_array($shippingMethods)) if(array_key_exists($selectedShippingMethod, $shippingMethods)) $checkoutOk = true; $noRight = true; $docTitle = "Shopping Cart"; $headCode = ' '; #$headCode .= ''; include SITE_HEADER; if($login->isUserloggedin()) include DIR_CART_VIEWS.'tpl_customer_bar.php'; include DIR_CART_VIEWS.'tpl_cart.php'; $canCheckout = $cart->checkoutOK($cart->shippingNeeded(),$checkoutOk,$allItemsShipFree,count($products)); include DIR_CART_VIEWS.'tpl_checkout_line.php'; echo $html; if(is_array($render)) foreach($render as $file) include DIR_CART_VIEWS.$file.'.php'; #echo 'Cart Id: '.$session; echo ''; if($settings->truecart_coupons AND $couponProductAvailable) include DIR_CART_VIEWS.'tpl_cart_coupons.php'; echo ''; include SITE_FOOTER; echo $session; ?>