public static function getUserCart($user_id, $asArray = false, $course = 1) { $rows = Cart::find()->where(['user_id' => $user_id])->all(); if ($asArray) { $new_rows = []; foreach ($rows as $key => $row) { $new_rows[] = ['title' => $row->title, 'price' => $row->price * $course, 'quantity' => $row->quantity, 'sub_total' => $row->sub_total * $course, 'product_type' => $row->product_type, 'product_info' => json_decode($row->product_info, true)]; } $rows = $new_rows; } return $rows; }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Cart::find(); $query->orderBy(['created_at' => SORT_DESC]); $dataProvider = new ActiveDataProvider(['query' => $query]); if ($this->load($params) && !$this->validate()) { return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'user_id' => $this->user_id, 'product_id' => $this->product_id, 'number' => $this->number, 'market_price' => $this->market_price, 'price' => $this->price, 'type' => $this->type, 'created_at' => $this->created_at, 'updated_at' => $this->updated_at]); $query->andFilterWhere(['like', 'session_id', $this->session_id])->andFilterWhere(['like', 'sku', $this->sku])->andFilterWhere(['like', 'name', $this->name])->andFilterWhere(['like', 'thumb', $this->thumb]); return $dataProvider; }
/** * 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); }
/** * Lists all Cart models. * @return mixed */ public function actionIndex() { $dataProvider = new ActiveDataProvider(['query' => Cart::find()]); return $this->render('index', ['dataProvider' => $dataProvider]); }
public function actionAjaxAdd() { Yii::$app->response->format = Response::FORMAT_JSON; $productId = Yii::$app->request->post('productId'); $number = Yii::$app->request->post('number'); if ($productId && $number) { // 如果购物车已有,则更新,否则在购物车中增加 if ($cart = Cart::find()->where(['and', 'product_id=' . $productId, ['or', 'session_id="' . Yii::$app->session->id . '"', 'user_id=' . Yii::$app->user->isGuest ? 0 : Yii::$app->user->id]])->one()) { $product = Product::findOne($productId); if ($cart->number + $number <= $product->stock) { $cart->updateAllCounters(['number' => $number], ['and', 'product_id=' . $productId, ['or', 'session_id="' . Yii::$app->session->id . '"', 'user_id=' . Yii::$app->user->isGuest ? 0 : Yii::$app->user->id]]); return ['status' => 1, 'productId' => $productId, 'number' => $number]; } else { return ['status' => -2, 'productId' => $productId, 'number' => $number]; } } elseif ($model = Product::findOne($productId)) { if ($model->stock >= $number) { $cart = new Cart(); $cart->session_id = Yii::$app->session->id; $cart->user_id = Yii::$app->user->isGuest ? 0 : Yii::$app->user->id; $cart->product_id = $productId; $cart->number = $number; $cart->sku = $model->sku; $cart->name = $model->name; $cart->market_price = $model->market_price; $cart->price = $model->price; $cart->thumb = $model->thumb; $cart->type = $model->type; $cart->save(); return ['status' => 1, 'productId' => $productId, 'number' => $number]; } else { return ['status' => -2, 'productId' => $productId, 'number' => $number]; } } } return ['status' => -1, 'productId' => $productId, 'number' => $number]; }