Exemplo n.º 1
0
 public function actionCoupon()
 {
     if ($_GET['remove'] && !empty($_GET['remove'])) {
         cart_discount::model()->deleteAllByAttributes(array('discount_ID' => $_GET['remove'], 'cart_ID' => Yii::app()->user->getState('cart_ID')));
         Yii::app()->user->setFlash('cart', "voucher has been removed");
         $this->redirect(array('index'));
         exit;
     }
     if ($_POST['token'] != md5(Yii::app()->user->getStateKeyPrefix())) {
         $this->redirect(array('index'));
         exit;
     }
     if (!$_POST['code'] or empty($_POST['code'])) {
         Yii::app()->user->setFlash('cart', "voucher name can't be blank");
         $this->redirect(array('index'));
         exit;
     }
     if (!($discount = discount_entity::model()->findByAttributes(array('discount_name' => $_POST['code'])))) {
         Yii::app()->user->setFlash('cart', "voucher name not valid");
         $this->redirect(array('index'));
         exit;
     }
     $cart = cart::model()->findByPk(Yii::app()->user->getState('cart_ID'));
     if ($msg = $cart->validateDiscount($_POST['total'], $discount->discount_ID, true)) {
         Yii::app()->user->setFlash('cart', $msg);
         $this->redirect(array('index'));
         exit;
     } else {
         $cartDiscount = new cart_discount();
         $cartDiscount->cart_ID = $cart->cart_ID;
         $cartDiscount->discount_ID = $discount->discount_ID;
         $cartDiscount->save();
     }
     $this->redirect(array('index'));
 }
Exemplo n.º 2
0
 public function checkDiscounts($orderTotal)
 {
     if (!$orderTotal > 0) {
         cart_discount::model()->deleteAllByAttributes(array('cart_ID' => $this->cart_ID));
         return;
     }
     if (sizeof($discounts = cart_discount::items($this->cart_ID)) >= 1) {
         foreach ($discounts as $discount) {
             $minimal = product_entity::decoratePrice($discount['discount_minmal']);
             if ($orderTotal < $minimal) {
                 cart_discount::model()->deleteAllByAttributes(array('cart_ID' => $this->cart_ID, 'discount_ID' => $discount['discount_ID']));
                 break;
             }
             if (!$discount['discount_active'] == 1 or !$discount['discount_quantity']) {
                 cart_discount::model()->deleteAllByAttributes(array('cart_ID' => $this->cart_ID, 'discount_ID' => $discount['discount_ID']));
                 break;
             }
             if (strtotime($discount['discount_from']) > time() or strtotime($discount['discount_to']) < time()) {
                 cart_discount::model()->deleteAllByAttributes(array('cart_ID' => $this->cart_ID, 'discount_ID' => $discount['discount_ID']));
                 break;
             }
             if ($this->nbDiscounts($discount['discount_ID']) >= $discount['discount_quantity_per_user'] or order_discount::nbDiscountCustomer(Yii::app()->user->getId(), $discount['discount_ID']) + $this->nbDiscounts($discount['discount_ID']) >= $discount['discount_quantity_per_user']) {
                 cart_discount::model()->deleteAllByAttributes(array('cart_ID' => $this->cart_ID, 'discount_ID' => $discount['discount_ID']));
                 break;
             }
             $onlyProductWithDiscount = true;
             if (!$discount['discount_cumulable_reduction'] == 1) {
                 foreach ($this->products as $product) {
                     if (!intval($product['product_reducetion_price']) and !intval($product['product_reducetion_percent'])) {
                         $onlyProductWithDiscount = false;
                         break;
                     }
                 }
             }
             if (!$discount['discount_cumulable_reduction'] == 1 and $onlyProductWithDiscount) {
                 cart_discount::model()->deleteAllByAttributes(array('cart_ID' => $this->cart_ID, 'discount_ID' => $discount['discount_ID']));
                 break;
             }
         }
     }
     return;
 }