public function preProcess()
 {
     global $isVirtualCart, $orderTotal;
     parent::preProcess();
     /* If some products have disappear */
     if (!self::$cart->checkQuantities()) {
         $this->step = 0;
         $this->errors[] = Tools::displayError('An item in your cart is no longer available for this quantity, you cannot proceed with your order.');
     }
     /* Check minimal amount */
     $currency = Currency::getCurrency((int) self::$cart->id_currency);
     $orderTotal = self::$cart->getOrderTotal();
     $minimalPurchase = Tools::convertPrice((double) Configuration::get('PS_PURCHASE_MINIMUM'), $currency);
     if (self::$cart->getOrderTotal(false, Cart::ONLY_PRODUCTS) < $minimalPurchase && $this->step != -1) {
         $this->step = 0;
         $this->errors[] = Tools::displayError('A minimum purchase total of') . ' ' . Tools::displayPrice($minimalPurchase, $currency) . ' ' . Tools::displayError('is required in order to validate your order.');
     }
     if (!self::$cookie->isLogged(true) and in_array($this->step, array(1, 2, 3))) {
         Tools::redirect('authentication.php?back=' . urlencode('order.php?step=' . $this->step));
     }
     if ($this->nbProducts) {
         self::$smarty->assign('virtual_cart', $isVirtualCart);
     }
     // Update carrier selected on preProccess in order to fix a bug of
     // block cart when it's hooked on leftcolumn
     if ($this->step == 3 && Tools::isSubmit('processCarrier')) {
         $this->processCarrier();
     }
 }
 public function preProcess()
 {
     global $isVirtualCart, $orderTotal;
     parent::preProcess();
     /* If some products have disappear */
     if (!self::$cart->checkQuantities()) {
         $this->step = 0;
         $this->errors[] = Tools::displayError('An item in your shopping bag is no longer available for this quantity, please remove it to proceed.');
     }
     /* Check minimal amount */
     $currency = Currency::getCurrency((int) self::$cart->id_currency);
     $orderTotal = self::$cart->getOrderTotal();
     $minimalPurchase = Tools::convertPrice((double) Configuration::get('PS_PURCHASE_MINIMUM'), $currency);
     if (self::$cart->getOrderTotal(false) < $minimalPurchase && $this->step != -1) {
         $this->step = 0;
         $this->errors[] = Tools::displayError('A minimum purchase total of') . ' ' . Tools::displayPrice($minimalPurchase, $currency) . ' ' . Tools::displayError('is required in order to validate your order.');
     }
     if (!self::$cookie->isLogged(true) and in_array($this->step, array(1, 2, 3))) {
         Tools::redirect('authentication.php?back=' . urlencode('order.php?step=' . $this->step));
     }
     if ($this->nbProducts) {
         self::$smarty->assign('virtual_cart', $isVirtualCart);
     }
     $this->_addAddress($this->step);
     if (self::$cookie->isLogged(true)) {
         $reward_points = VBRewards::getCustomerPoints(self::$cookie->id_customer);
         $redemption_status = VBRewards::checkPointsValidity(self::$cookie->id_customer, 0, self::$cart->getOrderTotal(true, Cart::ONLY_PRODUCTS));
         self::$smarty->assign('can_redeem_points', 1);
         self::$smarty->assign("redemption_status", $redemption_status);
         if ($redemption_status === CANNOT_REDEEM_COINS) {
             self::$smarty->assign('can_redeem_points', 0);
         } else {
             if ($redemption_status === INSUFFICIENT_VALID_ORDERS) {
                 self::$smarty->assign('redemption_status_message', 'Coins can be redeemed from second purchase onwards.');
             } else {
                 if ($redemption_status === MIN_CRITERIA_NOT_MET) {
                     self::$smarty->assign('redemption_status_message', 'Order value should be more than 100 USD to redeem coins');
                 }
             }
         }
         self::$smarty->assign('redeem_points', (int) self::$cart->getPoints());
         self::$smarty->assign('balance_points', $reward_points);
         if ($reward_points - (int) self::$cart->getPoints() > 0) {
             self::$smarty->assign('balance_cash', (int) self::$cart->getPointsDiscounts($reward_points - (int) self::$cart->getPoints()));
         }
     }
 }
 public function preProcess()
 {
     global $isVirtualCart, $orderTotal;
     parent::preProcess();
     /* If some products have disappear */
     if (!self::$cart->checkQuantities()) {
         $this->step = 0;
         $this->errors[] = Tools::displayError('An item in your cart is no longer available for this quantity, you cannot proceed with your order.');
     }
     /* Check minimal amount */
     $currency = Currency::getCurrency((int) self::$cart->id_currency);
     $orderTotal = self::$cart->getOrderTotal();
     $minimalPurchase = Tools::convertPrice((double) Configuration::get('PS_PURCHASE_MINIMUM'), $currency);
     if ($orderTotal < $minimalPurchase) {
         $this->step = 0;
         $this->errors[] = Tools::displayError('A minimum purchase total of') . ' ' . Tools::displayPrice($minimalPurchase, $currency) . ' ' . Tools::displayError('is required in order to validate your order.');
     }
     if (!self::$cookie->isLogged(true) and in_array($this->step, array(1, 2, 3))) {
         Tools::redirect('authentication.php?back=order.php?step=' . $this->step);
     }
     if ($this->nbProducts) {
         self::$smarty->assign('virtual_cart', $isVirtualCart);
     }
 }
 public function preProcess()
 {
     parent::preProcess();
     if ($this->nbProducts) {
         self::$smarty->assign('virtual_cart', false);
     }
     $this->isLogged = (bool) ((int) self::$cookie->id_customer and Customer::customerIdExistsStatic((int) self::$cookie->id_customer));
     if (self::$cart->nbProducts()) {
         if (Tools::isSubmit('ajax')) {
             if (Tools::isSubmit('method')) {
                 switch (Tools::getValue('method')) {
                     case 'updateMessage':
                         if (Tools::isSubmit('message')) {
                             $txtMessage = urldecode(Tools::getValue('message'));
                             $this->_updateMessage($txtMessage);
                             if (sizeof($this->errors)) {
                                 die('{"hasError" : true, "errors" : ["' . implode('\',\'', $this->errors) . '"]}');
                             }
                             die(true);
                         }
                         break;
                     case 'updateCarrierAndGetPayments':
                         if (Tools::isSubmit('id_carrier') and Tools::isSubmit('recyclable') and Tools::isSubmit('gift') and Tools::isSubmit('gift_message')) {
                             if ($this->_processCarrier()) {
                                 $return = array('summary' => self::$cart->getSummaryDetails(), 'HOOK_TOP_PAYMENT' => Module::hookExec('paymentTop'), 'HOOK_PAYMENT' => $this->_getPaymentMethods());
                                 die(Tools::jsonEncode($return));
                             } else {
                                 $this->errors[] = Tools::displayError('Error occurred updating cart.');
                             }
                             if (sizeof($this->errors)) {
                                 die('{"hasError" : true, "errors" : ["' . implode('\',\'', $this->errors) . '"]}');
                             }
                             exit;
                         }
                         break;
                     case 'updateTOSStatusAndGetPayments':
                         if (Tools::isSubmit('checked')) {
                             self::$cookie->checkedTOS = (int) Tools::getValue('checked');
                             die(Tools::jsonEncode(array('HOOK_TOP_PAYMENT' => Module::hookExec('paymentTop'), 'HOOK_PAYMENT' => $this->_getPaymentMethods())));
                         }
                         break;
                     case 'getCarrierList':
                         die(Tools::jsonEncode($this->_getCarrierList()));
                         break;
                     case 'editCustomer':
                         if (!$this->isLogged) {
                             exit;
                         }
                         $customer = new Customer((int) self::$cookie->id_customer);
                         if (Tools::getValue('years')) {
                             $customer->birthday = (int) Tools::getValue('years') . '-' . (int) Tools::getValue('months') . '-' . (int) Tools::getValue('days');
                         }
                         $_POST['lastname'] = $_POST['customer_lastname'];
                         $_POST['firstname'] = $_POST['customer_firstname'];
                         $this->errors = $customer->validateControler();
                         $customer->newsletter = (int) Tools::isSubmit('newsletter');
                         $customer->optin = (int) Tools::isSubmit('optin');
                         $return = array('hasError' => !empty($this->errors), 'errors' => $this->errors, 'id_customer' => (int) self::$cookie->id_customer, 'token' => Tools::getToken(false));
                         if (!sizeof($this->errors)) {
                             $return['isSaved'] = (bool) $customer->update();
                         } else {
                             $return['isSaved'] = false;
                         }
                         die(Tools::jsonEncode($return));
                         break;
                     case 'getAddressBlockAndCarriersAndPayments':
                         if (self::$cookie->isLogged()) {
                             // check if customer have addresses
                             if (!Customer::getAddressesTotalById((int) self::$cookie->id_customer)) {
                                 die(Tools::jsonEncode(array('no_address' => 1)));
                             }
                             if (file_exists(_PS_MODULE_DIR_ . 'blockuserinfo/blockuserinfo.php')) {
                                 include_once _PS_MODULE_DIR_ . 'blockuserinfo/blockuserinfo.php';
                                 $blockUserInfo = new BlockUserInfo();
                             }
                             self::$smarty->assign('isVirtualCart', self::$cart->isVirtualCart());
                             $this->_processAddressFormat();
                             $this->_assignAddress();
                             // Wrapping fees
                             $wrapping_fees = (double) Configuration::get('PS_GIFT_WRAPPING_PRICE');
                             $wrapping_fees_tax = new Tax((int) Configuration::get('PS_GIFT_WRAPPING_TAX'));
                             $wrapping_fees_tax_inc = $wrapping_fees * (1 + (double) $wrapping_fees_tax->rate / 100);
                             $return = array('summary' => self::$cart->getSummaryDetails(), 'order_opc_adress' => self::$smarty->fetch(_PS_THEME_DIR_ . 'order-address.tpl'), 'block_user_info' => isset($blockUserInfo) ? $blockUserInfo->hookTop(array()) : '', 'carrier_list' => $this->_getCarrierList(), 'HOOK_TOP_PAYMENT' => Module::hookExec('paymentTop'), 'HOOK_PAYMENT' => $this->_getPaymentMethods(), 'no_address' => 0, 'gift_price' => Tools::displayPrice(Tools::convertPrice(Product::getTaxCalculationMethod() == 1 ? $wrapping_fees : $wrapping_fees_tax_inc, new Currency((int) self::$cookie->id_currency))));
                             die(Tools::jsonEncode($return));
                         }
                         die(Tools::displayError());
                         break;
                     case 'makeFreeOrder':
                         /* Bypass payment step if total is 0 */
                         if ($id_order = $this->_checkFreeOrder() and $id_order) {
                             $email = self::$cookie->email;
                             if (self::$cookie->is_guest) {
                                 self::$cookie->logout();
                             }
                             // If guest we clear the cookie for security reason
                             die('freeorder:' . $id_order . ':' . $email);
                         }
                         exit;
                         break;
                     case 'updateAddressesSelected':
                         if (self::$cookie->isLogged(true)) {
                             $id_address_delivery = (int) Tools::getValue('id_address_delivery');
                             $id_address_invoice = (int) Tools::getValue('id_address_invoice');
                             $address_delivery = new Address((int) Tools::getValue('id_address_delivery'));
                             $address_invoice = (int) Tools::getValue('id_address_delivery') == (int) Tools::getValue('id_address_invoice') ? $address_delivery : new Address((int) Tools::getValue('id_address_invoice'));
                             if ($address_delivery->id_customer != self::$cookie->id_customer || $address_invoice->id_customer != self::$cookie->id_customer) {
                                 $this->errors[] = Tools::displayError('This address is not yours.');
                             } elseif (!Address::isCountryActiveById((int) Tools::getValue('id_address_delivery'))) {
                                 $this->errors[] = Tools::displayError('This address is not in a valid area.');
                             } elseif (!Validate::isLoadedObject($address_delivery) or !Validate::isLoadedObject($address_invoice) or $address_invoice->deleted or $address_delivery->deleted) {
                                 $this->errors[] = Tools::displayError('This address is invalid.');
                             } else {
                                 self::$cart->id_address_delivery = (int) Tools::getValue('id_address_delivery');
                                 self::$cart->id_address_invoice = Tools::isSubmit('same') ? self::$cart->id_address_delivery : (int) Tools::getValue('id_address_invoice');
                                 if (!self::$cart->update()) {
                                     $this->errors[] = Tools::displayError('An error occurred while updating your cart.');
                                 }
                                 if (!sizeof($this->errors)) {
                                     if (self::$cookie->id_customer) {
                                         $customer = new Customer((int) self::$cookie->id_customer);
                                         $groups = $customer->getGroups();
                                     } else {
                                         $groups = array(1);
                                     }
                                     $result = $this->_getCarrierList();
                                     // Wrapping fees
                                     $wrapping_fees = (double) Configuration::get('PS_GIFT_WRAPPING_PRICE');
                                     $wrapping_fees_tax = new Tax((int) Configuration::get('PS_GIFT_WRAPPING_TAX'));
                                     $wrapping_fees_tax_inc = $wrapping_fees * (1 + (double) $wrapping_fees_tax->rate / 100);
                                     $result = array_merge($result, array('summary' => self::$cart->getSummaryDetails(), 'HOOK_TOP_PAYMENT' => Module::hookExec('paymentTop'), 'HOOK_PAYMENT' => $this->_getPaymentMethods(), 'gift_price' => Tools::displayPrice(Tools::convertPrice(Product::getTaxCalculationMethod() == 1 ? $wrapping_fees : $wrapping_fees_tax_inc, new Currency((int) self::$cookie->id_currency)))));
                                     die(Tools::jsonEncode($result));
                                 }
                             }
                             if (sizeof($this->errors)) {
                                 die('{"hasError" : true, "errors" : ["' . implode('\',\'', $this->errors) . '"]}');
                             }
                         }
                         die(Tools::displayError());
                         break;
                     default:
                         exit;
                 }
             }
             exit;
         }
     } elseif (Tools::isSubmit('ajax')) {
         exit;
     }
 }