/** * main action */ public function mainAction() { /** * find code */ if ($_SESSION['promotion_code']) { $code = $_SESSION['promotion_code']; } else { if (trim($_POST['promotion_code']) != '') { $code = trim($_POST['promotion_code']); } else { $code = false; } } /** * Check Actions */ if ($_POST['promotion_code_add'] && $code) { $_SESSION['promotion_code'] = $code; onxshopGoTo("/page/{$_SESSION['active_pages'][0]}"); } else { if ($_POST['promotion_code_remove']) { $_SESSION['promotion_code'] = false; onxshopGoTo("/page/{$_SESSION['active_pages'][0]}"); } } /** * initialize */ require_once 'models/ecommerce/ecommerce_promotion.php'; $Promotion = new ecommerce_promotion(); $Promotion->setCacheable(false); require_once 'models/ecommerce/ecommerce_basket.php'; $Basket = new ecommerce_basket(); $Basket->setCacheable(false); /** * basket detail */ if (is_numeric($_SESSION['basket']['id'])) { $basket = $Basket->getFullDetail($_SESSION['basket']['id']); $Basket->calculateBasketSubTotals($basket, $this->isVatEligible($basket['customer_id'])); $Basket->calculateBasketDiscount($basket, $_SESSION['promotion_code']); $Basket->saveDiscount($basket); } else { $basket = false; } /** * Allow Guest Checkout */ if ($basket['customer_id'] == 0 && $_SESSION['client']['customer']['guest'] == 1) { $customer_email = $_SESSION['client']['customer']['email']; } else { $customer_email = ''; } /** * Display */ if ($basket && ($promotion_code = $Promotion->checkCodeBeforeApply($code, $basket['customer_id'], $basket, $customer_email))) { $promotion_code['value'] = $code; $this->tpl->assign('PROMOTION_CODE', $promotion_code); if ($promotion_code['discount_percentage_value'] > 0) { $this->tpl->parse('content.applied.discount_percentage_value'); } if ($promotion_code['discount_fixed_value'] > 0) { $this->tpl->parse('content.applied.discount_fixed_value'); } if ($promotion_code['discount_free_delivery'] == 1) { $this->tpl->parse('content.applied.discount_free_delivery'); } if ($promotion_code['free_promo_product']) { $this->tpl->parse('content.applied.free_item_info'); } $this->tpl->parse('content.applied'); } else { //remove code $code = false; $promotion_code = array(); $promotion_code['value'] = $code; $this->tpl->assign('PROMOTION_CODE', $promotion_code); $this->tpl->parse('content.enter'); } /** * Save to session */ $_SESSION['promotion_code'] = $code; return true; }
/** * insert delivery */ function insertDelivery($order_data) { require_once 'models/ecommerce/ecommerce_basket.php'; $Basket = new ecommerce_basket(); $Basket->setCacheable(false); $basket = $Basket->getFullDetail($order_data['basket_id']); $include_vat = $this->isVatEligible($order_data['delivery_address_id'], $basket['customer_id']); $Basket->calculateBasketSubTotals($basket, $include_vat); $code = $order_data['other_data']['promotion_code']; $verify_code = false; $promotion_detail = $Basket->calculateBasketDiscount($basket, $code, $verify_code); require_once 'models/ecommerce/ecommerce_delivery.php'; $Delivery = new ecommerce_delivery(); $delivery = $Delivery->calculateDelivery($basket, $order_data['other_data']['delivery_options']['carrier_id'], $order_data['delivery_address_id'], $promotion_detail); //prepare object require_once 'models/ecommerce/ecommerce_delivery.php'; $Ecommerce_Delivery = new ecommerce_delivery(); //format data $delivery_data['order_id'] = $order_data['id']; $delivery_data['carrier_id'] = $order_data['other_data']['delivery_options']['carrier_id']; $delivery_data['value_net'] = $delivery['value_net']; $delivery_data['vat'] = $delivery['vat']; $delivery_data['vat_rate'] = $delivery['vat_rate']; $delivery_data['required_datetime'] = $order_data['other_data']['delivery_options']['required_datetime']; $delivery_data['note_customer'] = ''; $delivery_data['note_backoffice'] = ''; $delivery_data['other_data'] = $order_data['other_data']['delivery_options']['other_data']; $delivery_data['weight'] = $delivery['weight']; //insert if ($id = $Ecommerce_Delivery->insertDelivery($delivery_data)) { return $id; } else { return false; } }
/** * check if existing code can be used */ public function checkCodeBeforeApply($code, $customer_id, $basket, $customer_email = '') { if ($promotion_data = $this->checkCodeMatch($code)) { // make sure to avoid rounding errors $order_value = round($basket['sub_total']['price'], 2); /** * check if customer_id is needed (some codes can be used in guest checkout mode) */ if ($customer_id == 0 && ($promotion_data['uses_per_customer'] > 0 || $promotion_data['limit_cumulative_discount'] > 0 || $promotion_data['generated_by_customer_id'] > 0 || $promotion_data['limit_by_customer_id'] > 0)) { if (!Zend_Registry::isRegistered('ecommerce_promotion:login_needed')) { if ($_SESSION['client']['customer']['guest']) { msg("We're sorry, but this voucher code cannot be used in conjunction with “Guest Checkout”. Please go to" . " previous step and select “Save Details for Next Order” to create an account.", 'error'); } else { msg("You have to login or register to use your voucher code.", 'error'); } Zend_Registry::set('ecommerce_promotion:login_needed', true); } return false; } /** * first order */ if ($promotion_data['limit_to_first_order'] > 0) { if ($this->getNumCustomersOrders($customer_id, $customer_email) > 0) { if ($this->getNumCustomersPaidOrders($customer_id, $customer_email) == 0) { if (!Zend_Registry::isRegistered('ecommerce_promotion:first_order_unpaid')) { msg("Code \"{$code}\" can only be applied to your first order. If you cancelled " . "your previous order, please either return to it in “My Account” or contact " . "customer services if you wish to continue with your current order.", 'error'); Zend_Registry::set('ecommerce_promotion:first_order_unpaid', true); } } else { if (!Zend_Registry::isRegistered('ecommerce_promotion:first_order')) { msg("We are sorry, this voucher is only valid on your first order", 'error'); Zend_Registry::set('ecommerce_promotion:first_order', true); } } return false; } } /** * uses_per_coupon */ if ($promotion_data['uses_per_coupon'] > 0) { if ($this->getCountUsageOfSingleCode($code) + 1 > $promotion_data['uses_per_coupon']) { if (substr($promotion_data['code_pattern'], 0, 4) != "REF-") { // referral codes validity is extended automatically if (!Zend_Registry::isRegistered('ecommerce_promotion:total_usage_exceeded')) { msg("Code \"{$code}\" usage exceed number of allowed applications", 'error'); Zend_Registry::set('ecommerce_promotion:total_usage_exceeded', true); } return false; } } } /** * check uses_per_customer */ if ($promotion_data['uses_per_customer'] > 0) { if ($this->getCountUsageOfSingleCode($code, $customer_id) + 1 > $promotion_data['uses_per_customer']) { if (!Zend_Registry::isRegistered('ecommerce_promotion:per_user_usage_exceeded')) { msg("Code \"{$code}\" usage exceed number of allowed applications per one customer", 'error'); Zend_Registry::set('ecommerce_promotion:per_user_usage_exceeded', true); } return false; } } /** * check limit_cumulative_discount */ if ($promotion_data['limit_cumulative_discount'] > 0) { require_once 'models/ecommerce/ecommerce_basket.php'; $Basket = new ecommerce_basket(); $Basket->calculateBasketDiscount($basket, $code, false); $usage = $this->getUsage($promotion_data['id'], $customer_id); if ($usage && $usage['sum_discount'] + $basket['discount'] > $promotion_data['limit_cumulative_discount']) { $limit = money_format("%n", $promotion_data['limit_cumulative_discount']); $provided = money_format("%n", $usage['sum_discount']); if (!Zend_Registry::isRegistered('ecommerce_promotion:limit_cumulative_discount_exceeded')) { $msg = "Code \"{$code}\" is limited to maximum discount value of {$limit}. " . "You’ve already used of {$provided}"; if ($promotion_data['discount_percentage_value'] > 0) { $max_order = ($promotion_data['limit_cumulative_discount'] - $usage['sum_discount']) / ($promotion_data['discount_percentage_value'] / 100); if ($max_order > 0) { $max_order = money_format("%n", $max_order); $msg .= ", so your current order would exceed your allotted discount value. " . "Please try again with an order of no greater than {$max_order}"; } } $msg .= "."; msg($msg, 'error'); Zend_Registry::set('ecommerce_promotion:limit_cumulative_discount_exceeded', true); } return false; } } /** * not using self-generated code */ if ($promotion_data['generated_by_customer_id'] > 0 && $promotion_data['generated_by_customer_id'] == $customer_id) { if (!Zend_Registry::isRegistered('ecommerce_promotion:own_code')) { msg("You are not allowed to redeem your own code!"); Zend_Registry::set('ecommerce_promotion:own_code', true); } return false; } /** * code limited to customer_id */ if ($promotion_data['limit_by_customer_id'] > 0 && $promotion_data['limit_by_customer_id'] != $customer_id) { if (!Zend_Registry::isRegistered('ecommerce_promotion:limit_by_customer_id')) { msg("You are not allowed to redeem the code!"); Zend_Registry::set('ecommerce_promotion:limit_by_customer_id', true); } return false; } /** * minimum order amount */ if ($promotion_data['limit_to_order_amount'] > 0) { if ($order_value < $promotion_data['limit_to_order_amount']) { if (!Zend_Registry::isRegistered('ecommerce_promotion:order_amount')) { $amount = money_format("%n", $promotion_data['limit_to_order_amount']); $sub_total = money_format("%n", $order_value); msg("The voucher code \"{$code}\" is restricted to orders in amount of {$amount}. You have only {$sub_total} in you basket.", 'error'); Zend_Registry::set('ecommerce_promotion:order_amount', true); } return false; } } /** * do not allow to buy gift voucher */ if (substr($promotion_data['code_pattern'], 0, 4) == "REF-" || substr($promotion_data['code_pattern'], 0, 4) == "REW-") { $gift_voucher_product_id = (int) $this->getGiftVoucherProductId(); if ($gift_voucher_product_id > 0 && count($basket['items']) > 0) { foreach ($basket['items'] as $item) { if ($item['product']['id'] == $gift_voucher_product_id) { if (!Zend_Registry::isRegistered('ecommerce_promotion:gift_in_basket')) { msg("Sorry, voucher codes cannot be used to buy Gift Voucher Codes."); Zend_Registry::set('ecommerce_promotion:gift_in_basket', true); } return false; } } } } /** * check if limited products are in basket */ $limited_ids = explode(",", $promotion_data['limit_list_products']); if (strlen($promotion_data['limit_list_products']) > 0 && is_array($limited_ids)) { $prod = 0; if (count($basket['items']) > 0) { foreach ($basket['items'] as $item) { if (in_array($item['product']['id'], $limited_ids)) { $prod++; } } } if ($prod == 0) { if (!Zend_Registry::isRegistered('ecommerce_promotion:limit_to_products')) { msg("Sorry, the voucher code \"{$code}\" is limited to certain products, which you don't have in your basket."); Zend_Registry::set('ecommerce_promotion:limit_to_products', true); } return false; } } /** * check if free promo item can be addeded to order */ $promotion_data['free_promo_product'] = $this->checkForFreePromoItem($promotion_data, $order_value); require_once 'models/ecommerce/ecommerce_promotion_type.php'; $Type = new ecommerce_promotion_type(); $promotion_data['type'] = $Type->detail($promotion_data['type']); return $promotion_data; } else { return false; } }