/**
  * Creates a new Cart model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new Cart();
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         return $this->redirect(['view', 'id' => $model->id]);
     } else {
         return $this->render('create', ['model' => $model]);
     }
 }
Beispiel #2
0
 /**
  * Creates a new Cart model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     //if(!Yii::$app->user->can('createYourAuth')) throw new ForbiddenHttpException(Yii::t('app', 'No Auth'));
     $model = new Cart();
     $model->loadDefaultValues();
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         return $this->redirect(['view', 'id' => $model->id]);
     } else {
         return $this->render('create', ['model' => $model]);
     }
 }
Beispiel #3
0
 public function save()
 {
     //Yii::getLogger()->log('start save photobook:'.$this->id, YII_DEBUG);
     $cart = Cart::findOne(['id' => $this->id]);
     if (empty($cart)) {
         $cart = new Cart();
         $cart->user_id = $this->user_id;
         $cart->title = $this->title;
         $cart->price = $this->price;
         $cart->quantity = $this->quantity;
         $cart->sub_total = $this->sub_total;
         $cart->product_type = $this->product_type;
         if (empty($this->product_info)) {
             $this->product_info = [];
         }
         $cart->product_info = json_encode($this->product_info);
         Yii::getLogger()->log('save:', YII_DEBUG);
         if ($cart->save()) {
             $this->id = $cart->id;
             return $cart;
         } else {
             Yii::getLogger()->log('save error', YII_DEBUG);
         }
     } else {
         $cart->user_id = $this->user_id;
         $cart->title = $this->title;
         $cart->price = $this->price;
         $cart->quantity = $this->quantity;
         $cart->sub_total = $this->sub_total;
         $cart->product_type = $this->product_type;
         if (empty($this->product_info)) {
             $this->product_info = [];
         }
         $cart->product_info = json_encode($this->product_info);
         Yii::getLogger()->log('update:', YII_DEBUG);
         if ($cart->update()) {
             return $cart;
         } else {
             Yii::getLogger()->log('update error:' . print_r($cart, true), YII_DEBUG);
         }
     }
     return null;
 }
 public function actionAddToManyCart()
 {
     $arrResult = [];
     $iP = Yii::$app->session->id;
     $good_id = Yii::$app->request->post('good_id');
     $total = Yii::$app->request->post('total');
     // если есть этот товар в корзине то просто увеличить его количество на 1
     $isItemInCart = Cart::_isItemAlreadyInCart($good_id);
     if ($isItemInCart) {
         Cart::updateItemQuantityUpMany($good_id, $total);
     } else {
         $model = new Cart();
         $model->ip = Yii::$app->session->id;
         $model->goods_id = $good_id;
         $model->quantity = $total;
         $model->price = Goods::getPriceById($good_id);
         $model->category_id = Goods::getCategoryById($good_id);
         $model->brend_id = Goods::getBrendById($good_id);
         //$model->validate();
         //vd($model->getErrors());
         $model->save();
     }
     $quantityInCart = Cart::getQountAllByIp($iP);
     $arrResult['quantity'] = $quantityInCart;
     Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
     return $arrResult;
 }
Beispiel #5
0
 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];
 }