Пример #1
0
 public function actionValidate()
 {
     $couponId = Yii::$app->request->post('couponId');
     $shoppingCoupon = new ShoppingCoupon();
     if ($couponId != 0) {
         if (in_array($couponId, Yii::$app->getSession()->get($shoppingCoupon::SESSION_KEY))) {
             return $shoppingCoupon->getResult($couponId);
         } else {
             return Json::encode(['status' => 'fail']);
         }
     }
 }
Пример #2
0
 /**
  * use coupon before create order
  * @param $event
  */
 public function useCoupon($event)
 {
     /** @var  $order Order */
     $order = $event->sender;
     /** @var  $couponModel  Coupon */
     $couponModel = Coupon::findOne(Yii::$app->getSession()->get(self::SESSION_COUPON_MODEL_KEY));
     if ($couponModel) {
         $couponRuleModel = $couponModel->couponRule;
         $result = Json::decode($couponRuleModel->result);
         if ($result['type']) {
             $order->total_price = $order->total_price * $result['number'];
         } else {
             $order->total_price = $order->total_price - $result['number'];
         }
         switch ($result['shipping']) {
             case 1:
                 $order->shipping_fee = $order->shipping_fee - $result['shippingFee'];
                 break;
             case 2:
                 $order->shipping_fee = 0;
         }
         Event::on(Order::className(), Order::EVENT_AFTER_INSERT, [ShoppingCoupon::className(), 'updateCouponStatus'], ['couponModel' => $couponModel]);
     }
 }
Пример #3
0
 public static function attachEvents()
 {
     self::frontendRegister();
     Event::on(Order::className(), Order::EVENT_CHANGE_PRICE, [ShoppingCoupon::className(), 'useCoupon']);
 }