function redeemCoupon() { global $toC_Json, $osC_Language, $osC_Payment, $osC_ShoppingCart, $osC_Currencies, $osC_Payment; $osC_Language->load('checkout'); require_once 'includes/classes/coupon.php'; $toC_Coupon = new toC_Coupon($_POST['coupon_redeem_code']); $errors = array(); if (!$toC_Coupon->isExist()) { $errors[] = $osC_Language->get('error_coupon_not_exist'); } if (!$toC_Coupon->isValid()) { $errors[] = $osC_Language->get('error_coupon_not_valid'); } if (!$toC_Coupon->isDateValid()) { $errors[] = $osC_Language->get('error_coupon_invalid_date'); } if (!$toC_Coupon->isUsesPerCouponValid()) { $errors[] = $osC_Language->get('error_coupon_exceed_uses_per_coupon'); } if (!$toC_Coupon->isUsesPerCustomerValid()) { $errors[] = $osC_Language->get('error_coupon_exceed_uses_per_customer'); } if ($toC_Coupon->hasRestrictCategories() || $toC_Coupon->hasRestrictProducts()) { if (!$toC_Coupon->containRestrictProducts()) { $errors[] = $osC_Language->get('error_coupon_no_match_products'); } } if (!$toC_Coupon->checkMinimumOrderQuantity()) { $errors[] = $osC_Language->get('error_coupon_minimum_order_quantity'); } if (sizeof($errors) == 0) { $osC_ShoppingCart->setCouponCode($_POST['coupon_redeem_code']); $form = toC_Json_Checkout::_getPaymentMethodForm(); $response = array('success' => true, 'form' => $form, 'isTotalZero' => $osC_ShoppingCart->isTotalZero()); } else { $response = array('success' => false, 'errors' => $errors); } echo $toC_Json->encode($response); }
function addCoupon() { global $toC_Json, $osC_Database, $osC_Language, $osC_ShoppingCart, $osC_Currencies, $osC_Weight, $osC_Tax, $osC_Customer; $osC_Tax = new osC_Tax_Admin(); $osC_Weight = new osC_Weight(); $osC_Currencies = new osC_Currencies(); $osC_Language->load('checkout'); $Qorder = $osC_Database->query('select * from :table_orders where orders_id = :orders_id'); $Qorder->bindTable(':table_orders', TABLE_ORDERS); $Qorder->bindInt(':orders_id', $_REQUEST['orders_id']); $Qorder->execute(); require_once '../includes/classes/customer.php'; $osC_Customer = new osC_Customer(); $osC_Customer->setID($Qorder->value('customers_id ')); $osC_ShoppingCart = new toC_ShoppingCart_Adapter($_REQUEST['orders_id']); if (isset($_REQUEST['coupon_code']) && !empty($_REQUEST['coupon_code']) && count($osC_ShoppingCart->getProducts()) != 0) { require_once '../includes/classes/coupon.php'; $toC_Coupon = new toC_Coupon($_REQUEST['coupon_code']); $errors = array(); if (!$toC_Coupon->isExist()) { $errors[] = $osC_Language->get('error_coupon_not_exist'); } else { if (!$toC_Coupon->isValid()) { $errors[] = $osC_Language->get('error_coupon_not_valid'); } else { if (!$toC_Coupon->isDateValid()) { $errors[] = $osC_Language->get('error_coupon_invalid_date'); } else { if (!$toC_Coupon->isUsesPerCouponValid()) { $errors[] = $osC_Language->get('error_coupon_exceed_uses_per_coupon'); } else { if (!$toC_Coupon->isUsesPerCustomerValid()) { $errors[] = $osC_Language->get('error_coupon_exceed_uses_per_customer'); } else { if ($toC_Coupon->hasRestrictCategories() || $toC_Coupon->hasRestrictProducts()) { if (!$toC_Coupon->containRestrictProducts()) { $errors[] = $osC_Language->get('error_coupon_no_match_products'); } } else { if (!$toC_Coupon->checkMinimumOrderQuantity()) { $errors[] = $osC_Language->get('error_coupon_minimum_order_quantity'); } else { if ($osC_ShoppingCart->isTotalZero()) { $errors[] = $osC_Language->get('error_shopping_cart_total_zero'); } } } } } } } } if (sizeof($errors) == 0) { $osC_ShoppingCart->setCouponCode($_REQUEST['coupon_code']); $response = array('success' => true, 'feedback' => $osC_Language->get('ms_success_action_performed')); } else { $response = array('success' => false, 'feedback' => implode('<br />', $errors)); } } else { $response = array('success' => false, 'feedback' => $osC_Language->get('ms_error_action_not_performed')); } echo $toC_Json->encode($response); }
function process() { global $osC_ShoppingCart, $osC_Currencies, $osC_Tax; $coupon_code = $osC_ShoppingCart->getCouponCode(); if (empty($coupon_code)) { return; } require_once realpath(dirname(__FILE__) . '/../../') . '/classes/coupon.php'; $toC_Coupon = new toC_Coupon($coupon_code); $coupon_amount = 0; $coupon_tax = 0; if ($toC_Coupon->isFreeShippingCoupon()) { $coupon_amount = $osC_ShoppingCart->getShippingMethod('cost'); $osC_ShoppingCart->addToTotal($coupon_amount * -1); if ($osC_ShoppingCart->getShippingMethod('tax_class_id') > 0) { $tax = $osC_Tax->getTaxRate($osC_ShoppingCart->getShippingMethod('tax_class_id'), $osC_ShoppingCart->getShippingAddress('country_id'), $osC_ShoppingCart->getShippingAddress('zone_id')); $tax_description = $osC_Tax->getTaxRateDescription($osC_ShoppingCart->getShippingMethod('tax_class_id'), $osC_ShoppingCart->getShippingAddress('country_id'), $osC_ShoppingCart->getShippingAddress('zone_id')); if (DISPLAY_PRICE_WITH_TAX == '1') { $coupon_tax = $osC_Tax->calculate($osC_ShoppingCart->getShippingMethod('cost') / (1 + $tax / 100), $tax); } else { $coupon_tax = $osC_Tax->calculate($osC_ShoppingCart->getShippingMethod('cost'), $tax); $osC_ShoppingCart->addToTotal($coupon_tax * -1); } $osC_ShoppingCart->addTaxAmount($coupon_tax * -1); $osC_ShoppingCart->addTaxGroup($tax_description, $coupon_tax * -1); if (DISPLAY_PRICE_WITH_TAX == '1') { $coupon_amount += $coupon_tax; } } } else { $valid_order_total = 0; $valid_tax = 0; $tax_groups = array(); $products = array(); $has_restrict_products = $toC_Coupon->hasRestrictProducts(); if ($has_restrict_products) { $products = $toC_Coupon->getRestrictProducts(); } foreach ($osC_ShoppingCart->getProducts() as $product) { if (in_array($product['id'], $products) && $has_restrict_products === true || $has_restrict_products === false) { $valid_order_total += $product['final_price'] * $product['quantity']; $products_tax = $osC_Tax->getTaxRate($product['tax_class_id'], $osC_ShoppingCart->getTaxingAddress('country_id'), $osC_ShoppingCart->getTaxingAddress('zone_id')); $products_tax_description = $osC_Tax->getTaxRateDescription($product['tax_class_id'], $osC_ShoppingCart->getTaxingAddress('country_id'), $osC_ShoppingCart->getTaxingAddress('zone_id')); $valid_tax += $osC_Tax->calculate($product['final_price'], $products_tax) * $product['quantity']; if (isset($tax_groups[$products_tax_description])) { $tax_groups[$products_tax_description] += $osC_Tax->calculate($product['final_price'], $products_tax) * $product['quantity']; } else { $tax_groups[$products_tax_description] = $osC_Tax->calculate($product['final_price'], $products_tax) * $product['quantity']; } if ($toC_Coupon->isIncludeTax() == true) { $valid_order_total += $osC_Tax->calculate($product['final_price'], $products_tax) * $product['quantity']; } } } if ($toC_Coupon->isIncludeShipping() == true) { $valid_order_total += $osC_ShoppingCart->getShippingMethod('cost'); if ($osC_ShoppingCart->getShippingMethod('tax_class_id') > 0) { $tax = $osC_Tax->getTaxRate($osC_ShoppingCart->getShippingMethod('tax_class_id'), $osC_ShoppingCart->getTaxingAddress('country_id'), $osC_ShoppingCart->getTaxingAddress('zone_id')); if (DISPLAY_PRICE_WITH_TAX == '1') { $shipping_tax = $osC_Tax->calculate($osC_ShoppingCart->getShippingMethod('cost') / (1 + $tax / 100), $tax); } else { $shipping_tax = $osC_Tax->calculate($osC_ShoppingCart->getShippingMethod('cost'), $tax); } $tax_description = $osC_Tax->getTaxRateDescription($osC_ShoppingCart->getShippingMethod('tax_class_id'), $osC_ShoppingCart->getShippingAddress('country_id'), $osC_ShoppingCart->getShippingAddress('zone_id')); $valid_tax += $shipping_tax; $tax_groups[$products_tax_description] += $shipping_tax; if ($toC_Coupon->isIncludeTax() == true) { $valid_order_total += $shipping_tax; } } } if ($toC_Coupon->isAmountCoupon()) { $coupon_amount = min($valid_order_total, $toC_Coupon->getCouponAmount()); } else { if ($toC_Coupon->isPercentageCoupon()) { $coupon_amount = round($toC_Coupon->getCouponAmount() * $valid_order_total / 100, 2); } } $osC_ShoppingCart->addToTotal($coupon_amount * -1); if ($toC_Coupon->isIncludeTax() == true) { $ratio = $valid_order_total == 0 ? 0 : $coupon_amount / $valid_order_total; foreach ($osC_ShoppingCart->_tax_groups as $key => $value) { if (isset($tax_groups[$key])) { $coupon_tax += $tax_groups[$key] * $ratio; $osC_ShoppingCart->addTaxAmount($coupon_tax * -1); $osC_ShoppingCart->addTaxGroup($key, $coupon_tax * -1); $coupon_amount -= $coupon_tax; } if (DISPLAY_PRICE_WITH_TAX == '1') { $coupon_amount += $coupon_tax; } } } else { if ($toC_Coupon->isIncludeTax() == false) { $ratio = $coupon_amount / $valid_order_total; foreach ($osC_ShoppingCart->_tax_groups as $key => $value) { if (isset($tax_groups[$key])) { $coupon_tax += $tax_groups[$key] * $ratio; $osC_ShoppingCart->addTaxAmount($coupon_tax * -1); $osC_ShoppingCart->addTaxGroup($key, $coupon_tax * -1); $osC_ShoppingCart->addToTotal($coupon_tax * -1); } if (DISPLAY_PRICE_WITH_TAX == '1') { $coupon_amount += $coupon_tax; } } } } } if ($osC_ShoppingCart->isTotalZero()) { $osC_ShoppingCart->resetBillingMethod(false); } $osC_ShoppingCart->setCouponAmount($coupon_amount); if ($coupon_amount > 0) { $this->output[] = array('title' => $this->_title . ' (' . $coupon_code . ') : ', 'text' => '-' . $osC_Currencies->format($coupon_amount), 'value' => $coupon_amount * -1); } else { $osC_ShoppingCart->setCouponCode(null, false); } }