/** * This method is called after the user is successfully logged in. * The default implementation will trigger the [[EVENT_AFTER_LOGIN]] event. * If you override this method, make sure you call the parent implementation * so that the event is triggered. * @param IdentityInterface $identity the user identity information * @param boolean $cookieBased whether the login is cookie-based * @param integer $duration number of seconds that the user can remain in logged-in status. * If 0, it means login till the user closes the browser or the session is manually destroyed. */ protected function afterLogin($identity, $cookieBased, $duration) { $carts = Cart::find()->where(['session_id' => $this->oldSessionId])->all(); foreach ($carts as $cart) { $exist = Cart::find()->where(['user_id' => $identity->id, 'product_id' => $cart->product_id])->one(); if ($exist) { Cart::updateAllCounters(['number' => $cart->number], ['user_id' => $identity->id]); Cart::deleteAll(['session_id' => $this->oldSessionId, 'product_id' => $cart->product_id]); } else { Cart::updateAll(['session_id' => Yii::$app->session->id, 'user_id' => $identity->id], ['session_id' => $this->oldSessionId, 'product_id' => $cart->product_id]); } } Cart::updateAll(['session_id' => Yii::$app->session->id], ['user_id' => $identity->id]); //Cart::updateAll(['session_id' => Yii::$app->session->id, 'user_id' => $identity->id], ['session_id' => $this->oldSessionId]); return parent::afterLogin($identity, $cookieBased, $duration); }
/** * Deletes an existing Cart model. * If deletion is successful, the browser will be redirected to the 'index' page. * @param integer $id * @return mixed */ public function actionClean() { //if(!Yii::$app->user->can('deleteYourAuth')) throw new ForbiddenHttpException(Yii::t('app', 'No Auth')); $time = mktime(0, 0, 0, date('m'), date('d') - date('w') + 1 - 7, date('Y')); Cart::deleteAll(['and', 'user_id = 0', 'created_at < ' . $time]); return $this->redirect(['index']); }
public function actionCheckout2() { $this->layout = 'layouts'; if (Yii::$app->user->isGuest) { $this->redirect(Url::toRoute(['photobooks/not-found'])); return; } $ref = Yii::$app->request->get('ref'); $id = Yii::$app->request->get('id'); $user_id = Yii::$app->user->identity->getId(); $user = User::findOne(['id' => Yii::$app->user->identity->getId()]); $course = CurrencyConvertor::getCurse($user->default_currency); $cart_rows = CartForm::getUserCart($user_id, true, $course); if (count($cart_rows) == 0) { Yii::$app->getSession()->setFlash('error', Yii::t('app', 'Корзина пуста. Вы не можите создать счет.')); $this->redirect(Url::toRoute(['photobooks/index'])); } $total = 0; //Считаем сумму foreach ($cart_rows as $key => $cart_row) { $total += $cart_row['sub_total']; } $invoice = new InvoiceForm(); $invoice->user_id = $user_id; $invoice->data = ['rows' => $cart_rows]; $invoice->currency = $user->default_currency; $invoice->payment_type = Invoice::TYPE_LIQPAY; $invoice->delivery_address = $user->delivery_address; $invoice->total = $total; $invoice->status = Invoice::STATUS_NEW; if (!$invoice->save()) { Yii::$app->getSession()->setFlash('error', Yii::t('app', 'Ну удалось записать в базу данных.')); $this->redirect(Url::toRoute(['photobooks/index'])); return; } //Имеем $invoice->id //Нужно его привязать к фотокнигам foreach ($cart_rows as $key => $cart_row) { $total += $cart_row['sub_total']; if ($cart_row['product_type'] == Cart::PRODUCT_PHOTOBOOK && !empty($cart_row['product_info']) && !empty($cart_row['product_info']['Photobook'])) { $pb_id = $cart_row['product_info']['Photobook']['id']; $photobook = new PhotobookForm(); if ($photobook->loadById($pb_id)) { $photobook->invoice_id = $invoice->id; $photobook->save(); } } } //Чистим корзину Cart::deleteAll(['user_id' => $user_id]); //Отправляем пользователя на счет $this->redirect(Url::toRoute(['photobooks/invoice', 'id' => $invoice->id])); //return $this->render('checkout', [ 'user_id'=>$user_id, 'cart_rows'=>$cart_rows, 'course'=>$course, 'default_currency'=>$user->default_currency]); }
public function actionDeleteCartRow() { \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; if (Yii::$app->user->isGuest) { return ['error' => ['msg' => Yii::t('app', 'Доступ закрыт')]]; } $cart_id = Yii::$app->request->get('id', 0); if ($cart_id <= 0) { return ['error' => ['msg' => Yii::t('app', 'Не верный id')]]; } $carForm = new CartForm(); if (!$carForm->loadById($cart_id)) { return ['error' => ['msg' => Yii::t('app', 'Запись не найдена')]]; } if (Yii::$app->user->identity->role != User::ROLE_ADMIN) { if ($carForm->user_id != Yii::$app->user->identity->getId()) { return ['error' => ['msg' => Yii::t('app', 'Ошибка прав доступа')]]; } } if ($carForm->product_type == Cart::PRODUCT_PHOTOBOOK && !empty($carForm->product_info) && !empty($carForm->product_info['Photobook'])) { $pb_id = $carForm->product_info['Photobook']['id']; $photobook = new PhotobookForm(); if ($photobook->loadById($pb_id)) { $photobook->setStatus(Photobook::STATUS_NEW); } } if (!Cart::deleteAll(['id' => $cart_id])) { return ['error' => ['msg' => Yii::t('app', 'Не удалось удалить строку')]]; } return ['response' => ['status' => true]]; }
public function actionDestroy() { Cart::deleteAll(['session_id' => Yii::$app->session->id]); $this->goHome(); }