コード例 #1
0
 * Delete old voucher
 **/
if (_PS_VERSION_ >= '1.5') {
    $discounts = $context->cart->getCartRules();
} else {
    $discounts = $cart->getDiscounts();
}
if (count($discounts)) {
    foreach ($discounts as $key => $val) {
        if (strcmp($val['name'], 'Fid\'Bag') === 0) {
            if (_PS_VERSION_ >= '1.5') {
                $voucher = new CartRule($val['id_cart_rule']);
            } else {
                $voucher = new Discount($val['id_discount']);
            }
            $voucher->delete();
        }
    }
}
/**
 * create voucher
 **/
if ($amount > 0) {
    if (_PS_VERSION_ >= '1.5') {
        $voucher = new CartRule();
        $voucher->free_shipping = false;
        $voucher->reduction_percent = false;
        $voucher->reduction_amount = $amount;
        $voucher->name = array();
        $languages = Language::getLanguages(true);
        foreach ($languages as $language) {
コード例 #2
0
 public function preProcess()
 {
     global $isVirtualCart, $cookie;
     parent::preProcess();
     // Redirect to the good order process
     if (Configuration::get('PS_ORDER_PROCESS_TYPE') == 0 and (strpos($_SERVER['PHP_SELF'], 'order.php') === false and strpos($_SERVER['PHP_SELF'], 'cart-summary-large.php') === false)) {
         Tools::redirect('order.php');
     }
     if (Configuration::get('PS_ORDER_PROCESS_TYPE') == 1 and strpos($_SERVER['PHP_SELF'], 'order-opc.php') === false) {
         if (isset($_GET['step']) and $_GET['step'] == 3) {
             Tools::redirect('order-opc.php?isPaymentStep=true');
         }
         Tools::redirect('order-opc.php');
     }
     if (Configuration::get('PS_CATALOG_MODE')) {
         $this->errors[] = Tools::displayError('This store has not accepted your new order.');
     }
     if (Tools::isSubmit('submitReorder') and $id_order = (int) Tools::getValue('id_order')) {
         $oldCart = new Cart(Order::getCartIdStatic((int) $id_order, (int) self::$cookie->id_customer));
         $duplication = $oldCart->duplicate();
         if (!$duplication or !Validate::isLoadedObject($duplication['cart'])) {
             $this->errors[] = Tools::displayError('Sorry, we cannot renew your order.');
         } elseif (!$duplication['success']) {
             $this->errors[] = Tools::displayError('Missing items - we are unable to renew your order');
         } else {
             self::$cookie->id_cart = $duplication['cart']->id;
             self::$cookie->write();
             if (Configuration::get('PS_ORDER_PROCESS_TYPE') == 1) {
                 Tools::redirect('order-opc.php');
             }
             Tools::redirect('order.php');
         }
     }
     if (Tools::isSubmit('submit_instructions')) {
         $instructions = Tools::getValue('special_instructions');
         self::$cart->gift_message = pSQL($instructions);
         self::$cart->update();
         Tools::redirect('order?step=3');
     }
     if ($this->nbProducts) {
         /* check discount validity */
         $cartDiscounts = self::$cart->getDiscounts();
         $discountInvalid = false;
         foreach ($cartDiscounts as $k => $cartDiscount) {
             if ($error = self::$cart->checkDiscountValidity(new Discount((int) $cartDiscount['id_discount']), $cartDiscounts, self::$cart->getOrderTotal(true, Cart::ONLY_PRODUCTS_WITHOUT_SHIPPING), self::$cart->getProducts())) {
                 self::$cart->deleteDiscount((int) $cartDiscount['id_discount']);
                 $discountInvalid = true;
             }
         }
         if ($discountInvalid) {
             Tools::redirect('order-opc.php');
         }
         if (Tools::getValue('redeem_points')) {
             $points = (int) Tools::getValue('redeem_points');
             if ($points < 1) {
                 self::$smarty->assign('redeem_points', $points);
                 $this->errors[] = Tools::displayError('You can redeem minimum of 1 coin in an order.');
             }
             $orderTotal = self::$cart->getOrderTotal(true, Cart::ONLY_PRODUCTS);
             $redemption_status = VBRewards::checkPointsValidity($cookie->id_customer, $points + self::$cart->getPoints(), $orderTotal);
             if ($redemption_status === CAN_REDEEM_COINS) {
                 self::$cart->addPoints($points);
                 self::$smarty->assign('redeem_points', (int) self::$cart->getPoints());
             } else {
                 if ($redemption_status === INSUFFICIENT_VALID_ORDERS) {
                     $this->errors[] = Tools::displayError('Coins can be redeemed from second purchase onwards.');
                 } else {
                     if ($redemption_status === MIN_CRITERIA_NOT_MET) {
                         $this->errors[] = Tools::displayError('Order value should be more than 100 USD to redeem coins');
                     }
                 }
             }
             $this->adjustPoints();
         } elseif (Tools::getValue('deletePoints')) {
             self::$cart->deletePoints();
         }
         if (Tools::isSubmit('submitAddDiscount') and Tools::getValue('discount_name')) {
             $discountName = Tools::getValue('discount_name');
             if (!Validate::isDiscountName($discountName)) {
                 $this->errors[] = Tools::displayError('Voucher name invalid.');
             } else {
                 $discount = new Discount((int) Discount::getIdByName($discountName));
                 if (Validate::isLoadedObject($discount)) {
                     if ($tmpError = self::$cart->checkDiscountValidity($discount, self::$cart->getDiscounts(), self::$cart->getOrderTotal(), self::$cart->getProducts(), true)) {
                         $this->errors[] = $tmpError;
                     }
                 } else {
                     $this->errors[] = Tools::displayError('Voucher name invalid.');
                 }
                 if (!sizeof($this->errors)) {
                     self::$cart->addDiscount((int) $discount->id);
                     Tools::redirect('order-opc.php');
                 }
             }
             self::$smarty->assign(array('errors' => $this->errors, 'discount_name' => Tools::safeOutput($discountName)));
             $this->adjustPoints();
         } elseif (isset($_GET['deleteDiscount']) and Validate::isUnsignedId($_GET['deleteDiscount'])) {
             self::$cart->deleteDiscount((int) $_GET['deleteDiscount']);
             Tools::redirect('order-opc.php');
         }
         /* Is there only virtual product in cart */
         if ($isVirtualCart = self::$cart->isVirtualCart()) {
             $this->_setNoCarrier();
         }
     }
     //if enough stock, show free gift message
     $free_products = array(66254, 66255, 66256);
     $sql = "select quantity from ps_product where id_product in (" . implode(",", $free_products) . ")";
     $result = Db::getInstance()->ExecuteS($sql);
     $free_gift_stock = $has_free_gift = false;
     foreach ($result as $row) {
         if (intval($row['quantity']) > 0) {
             $free_gift_stock = true;
             break;
         }
     }
     $total_products_wt = self::$cart->getOrderTotal(true, Cart::ONLY_PRODUCTS);
     $cv = Tools::convertPrice($total_products_wt, (int) self::$cookie->id_currency, false);
     /*if( $cv > 50) {	
     		if( $free_gift_stock ) {
     			$cart_products = self::$cart->getProducts();
     			foreach($cart_products as $cproduct) {
     				$id_product = $cproduct['id_product'];
     				if( in_array($id_product, $free_products )) {
     					$has_free_gift = true;
     					break;	
     				}
     			}
     		}
     		if( !$has_free_gift && $free_gift_stock ) {
     			self::$smarty->assign("spl_voucher_message","Here is the free <a href='/1222-free-gifts'>gift product</a> for you.Valid still stock lasts. Read <a href='/content/30-womens-day-special-discount'>T&C</a> here");
     		}
     	} else {
     		$free_products = array(66254, 66255, 66256);
     		foreach($free_products as $p)
     			self::$cart->deleteProduct($p);
     		self::$smarty->assign("spl_voucher_message","We have a free <a href='/1222-free-gifts'>gift product</a> for you if the shopping cart value is atleast USD 50.");
     	}
             self::$smarty->assign('back', Tools::safeOutput(Tools::getValue('back')));
     
             if (self::$cart->gift_message) {
                 self::$smarty->assign('cart_instructions', 1);
                 self::$smarty->assign('special_instructions', self::$cart->gift_message);
             }*/
     //buy1 get 1
     $products = self::$cart->getProducts(true);
     $offeredProducts = 0;
     // this is a very interesting array, keeps as many rows as there are items in your cart
     // if there is an item with quantity 2, it will have two rows in that array,
     // ie item will have as many rows as the quantity of the item in cart.
     $offerproducts = array();
     foreach ($products as $product) {
         $productObj = new Product($product['id_product'], true);
         $tags = $productObj->getTags(1);
         //print_r($productObj);
         if (in_array('buy1get1', $tags)) {
             self::$smarty->assign("spl_voucher_message", "You are eligible for Buy 1 Get 1 Offer");
             //counting the b1g1 products
             $offeredProducts = $offeredProducts + $product['cart_quantity'];
         } else {
             if (in_array('buy2get3', $tags)) {
                 self::$smarty->assign("spl_voucher_message", "You are eligible for buy 3 pay for 2 offer");
                 //counting the b1g1 products
                 $offeredProducts = $offeredProducts + $product['cart_quantity'];
             }
         }
         //adding rows to interesting array $b1g1products
         for ($q = 0; $q < $product['cart_quantity']; $q++) {
             array_push($offerproducts, $product);
         }
     }
     //sorting products in ascending order of price
     usort($offerproducts, function ($prod1, $prod2) {
         return $prod1['price'] - $prod2['price'];
     });
     ///echo "<pre>";print_r($offerproducts);
     if (in_array('buy1get1', $tags)) {
         $noOfferedAdvantageAvailable = (int) ($offeredProducts / 2);
     } elseif (in_array('buy2get3', $tags)) {
         $noOfferedAdvantageAvailable = (int) ($offeredProducts / 3);
     }
     $discount = 0;
     for ($p = 0; $p < $noOfferedAdvantageAvailable; $p++) {
         //print_r("offerproducts".$offerproducts[$p]['price']);
         $discount = $discount + $offerproducts[$p]['price'];
         //print_r($discount);
     }
     if ((int) self::$cart->id_currency != 2) {
         $discount = Tools::convertPrice($discount, (int) self::$cart->id_currency, false);
     }
     $discount = round($discount, 2);
     $b1g1DiscountInCart = null;
     $b2g3DiscountInCart = null;
     $allCartDiscounts = self::$cart->getDiscounts();
     //print_r($allCartDiscounts);
     foreach ($allCartDiscounts as $cartDiscount) {
         if (strpos($cartDiscount['name'], 'B1G1') === 0) {
             $b1g1DiscountInCart = new Discount((int) Discount::getIdByName($cartDiscount['name']));
             //echo "buy1get1";
         } else {
             if (strpos($cartDiscount['name'], 'B2G3') === 0) {
                 $b2g3DiscountInCart = new Discount((int) Discount::getIdByName($cartDiscount['name']));
                 //echo "buy2get3";
             }
         }
     }
     if (in_array('buy1get1', $tags)) {
         if ($discount > 0 && empty($b1g1DiscountInCart)) {
             $new_discount = new Discount();
             $new_discount->id_discount_type = 2;
             $new_discount->value = $discount;
             // coupon value
             $new_discount->name = "B1G1" . Tools::rand_string(5);
             $new_discount->id_currency = 2;
             $new_discount->quantity = 1;
             $new_discount->quantity_per_user = 0;
             $new_discount->date_from = date('Y-m-d H:i:s');
             $new_discount->date_to = '2015-08-10 20:00:00';
             //validity
             $new_discount->cumulable_reduction = 1;
             $languages = Language::getLanguages();
             foreach ($languages as $language) {
                 $new_discount->description[$language['id_lang']] = "Buy1 Get1";
             }
             // coupon description
             $new_discount->add();
             $id_discount = Discount::getIdByName($new_discount->name);
             self::$cart->addDiscount($id_discount);
         } else {
             if (!empty($b1g1DiscountInCart)) {
                 $b1g1DiscountInCart->value = (double) $discount;
                 // coupon value
                 if ($b1g1DiscountInCart->value > 0) {
                     $b1g1DiscountInCart->update();
                 } else {
                     $b1g1DiscountInCart->delete();
                 }
             }
         }
     } else {
         if (in_array('buy2get3', $tags)) {
             if ($discount > 0 && empty($b2g3DiscountInCart)) {
                 $new_discount = new Discount();
                 $new_discount->id_discount_type = 2;
                 $new_discount->value = $discount;
                 // coupon value
                 $new_discount->name = "B2G3" . Tools::rand_string(5);
                 $new_discount->id_currency = 2;
                 $new_discount->quantity = 1;
                 $new_discount->quantity_per_user = 0;
                 $new_discount->date_from = date('Y-m-d H:i:s');
                 $new_discount->date_to = '2015-08-10 20:00:00';
                 //validity
                 $new_discount->cumulable_reduction = 1;
                 $languages = Language::getLanguages();
                 foreach ($languages as $language) {
                     $new_discount->description[$language['id_lang']] = "Buy2 Get3";
                 }
                 // coupon description
                 $new_discount->add();
                 $id_discount = Discount::getIdByName($new_discount->name);
                 self::$cart->addDiscount($id_discount);
             } else {
                 if (!empty($b2g3DiscountInCart)) {
                     $b2g3DiscountInCart->value = (double) $discount;
                     // coupon value
                     if ($b2g3DiscountInCart->value > 0) {
                         $b2g3DiscountInCart->update();
                     } else {
                         $b2g3DiscountInCart->delete();
                     }
                 }
             }
         }
     }
     /*//pick3pay2
              $products = self::$cart->getProducts(true);
              //print_r($products);
     
              $nP3P2Products = 0;
             
             // this is a very interesting array, keeps as many rows as there are items in your cart
             // if there is an item with quantity 2, it will have two rows in that array, 
             // ie item will have as many rows as the quantity of the item in cart.
             $p3p2products = array();
             foreach($products as $product) {   
                 $productObj = new Product($product['id_product'], true);
                 $tags = $productObj->getTags(1);
                 if(in_array('pick3pay2', $tags)){
                     self::$smarty->assign("spl_voucher_message", "Pick 3 and Pay for 2 only offer");
                     $nP3P2Products = $nP3P2Products + $product['cart_quantity'];
                     
                     for($q = 0; $q < $product['cart_quantity']; $q++){
                         array_push($p3p2products, $product);
                     }  
                 }
             }
     
            
             
             //sorting products in ascending order of price
             usort($p3p2products, function($prod1, $prod2){
                 return $prod1['price'] - $prod2['price'];
             });
     
             $noP3P2AdvantageAvailable = (int)($nP3P2Products/3);
             print_r($noP3P2AdvantageAvailable);
             $discount = 0;
             for($p = 0; $p < $noP3P2AdvantageAvailable; $p++){
                 $discount = $discount + $p3p2products[$p]['price'];
             }
     
             if((int)self::$cart->id_currency != 2){
                 $discount = Tools::convertPrice($discount, (int)self::$cart->id_currency, false);
             }
             
             $discount = round($discount, 2);
     
             $p3p2DiscountInCart = null;
             $allCartDiscounts = self::$cart->getDiscounts();
     
             foreach ($allCartDiscounts as $cartDiscount) {
                 //print_r($cartDiscount);print_r('<br/>');die;
                 if(strpos('P3P2', $cartDiscount['name']) == 0){
                     $p3p2DiscountInCart = new Discount((int) (Discount::getIdByName($cartDiscount['name'])));
                 }
             }
             
             if($discount > 0 && empty($p3p2DiscountInCart)){
                 $new_discount = new Discount();
                 $new_discount->id_discount_type = 2;
                 $new_discount->value = $discount; // coupon value
                 $new_discount->name = "P3P2" . Tools::rand_string(5);
                 $new_discount->id_currency = 2;
                 $new_discount->quantity = 1;
                 $new_discount->quantity_per_user = 100;
                 $new_discount->date_from = date('Y-m-d H:i:s');
                 $new_discount->date_to = '2015-09-10 20:00:00'; //validity
                 $new_discount->cumulable_reduction = 1;
                 $languages = Language::getLanguages();
                 foreach ($languages as $language)
                     $new_discount->description[$language['id_lang']] = "Pick3 Pay2"; // coupon description
                 
                 $new_discount->add();
                 $id_discount = Discount::getIdByName($new_discount->name);
                 print_r($id_discount);
                 self::$cart->addDiscount($id_discount);
                 
             }else if(!empty($p3p2DiscountInCart)){
                 $p3p2DiscountInCart->value = (float) $discount; // coupon value
                 if($p3p2DiscountInCart->value > 0)
                     $p3p2DiscountInCart->update();
                 else
                     $p3p2DiscountInCart->delete();
             }*/
 }