/** * 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]); } }
/** * Finds the Coupon model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Coupon the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Coupon::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }