/** * get current user's coupon * @param $cartItems * @return array */ public function getCouponList($cartItems) { /** @var $coupon Coupon */ $coupons = Coupon::find()->where(['user_id' => \Yii::$app->user->id, 'status' => 1])->all(); $result = ['usable' => [\Yii::t('coupon', 'Please Select One')], 'useless' => []]; $couponIdArray = []; foreach ($coupons as $coupon) { if ($this->validate($coupon, $cartItems)) { $couponIdArray[] = $coupon->coupon_id; $result['usable'][$coupon->coupon_id] = $coupon->coupon_no; } else { $result['useless'][$coupon->coupon_id] = $coupon->coupon_no; } } Yii::$app->getSession()->set(self::SESSION_KEY, $couponIdArray); return $result; }
/** * add a coupon for a user * @return string */ public function actionAddCoupon() { $couponCode = Yii::$app->request->post('couponCode'); /**@var $couponModel \star\marketing\models\Coupon **/ $couponModel = Coupon::find()->where(['coupon_no' => $couponCode])->one(); if ($couponModel) { if ($couponModel->end_at > time() && $couponModel->status == 1 && $couponModel->user_id == 0) { $couponModel->user_id = Yii::$app->user->id; if ($couponModel->save()) { return Json::encode(['message' => Yii::t('coupon', 'add coupon success!')]); } } return Json::encode(['message' => Yii::t('coupon', 'the coupon is useless')]); } else { return Json::encode(['message' => Yii::t('coupon', 'the coupon is not exist')]); } }
/** * Displays a single Coupon model. * @param integer $id * @return mixed */ public function actionView($id) { $dataProvider = new ActiveDataProvider(['query' => Coupon::find()->where(['rule_id' => $id])]); return $this->render('index_coupon', ['dataProvider' => $dataProvider]); }