/** * Добавление продукта в корзину. * * @return array|\yii\web\Response */ public function actionAddProduct() { $rr = new RequestResponse(); if ($rr->isRequestAjaxPost()) { $product_id = \Yii::$app->request->post('product_id'); $quantity = \Yii::$app->request->post('quantity'); /** * @var ShopProduct $product */ $product = ShopProduct::find()->where(['id' => $product_id])->one(); if (!$product) { $rr->message = \skeeks\cms\shop\Module::t('app', 'This product is not found, it may be removed.'); return (array) $rr; } $shopBasket = ShopBasket::find()->where(['fuser_id' => \Yii::$app->shop->shopFuser->id, 'product_id' => $product_id, 'order_id' => null])->one(); if (!$shopBasket) { $shopBasket = new ShopBasket(['fuser_id' => \Yii::$app->shop->shopFuser->id, 'product_id' => $product->id, 'quantity' => 0]); } $shopBasket->quantity = $shopBasket->quantity + $quantity; if (!$shopBasket->recalculate()->save()) { $rr->success = false; $rr->message = \skeeks\cms\shop\Module::t('app', 'Failed to add item to cart'); } else { $rr->success = true; $rr->message = \skeeks\cms\shop\Module::t('app', 'Item added to cart'); } \Yii::$app->shop->shopFuser->link('site', \Yii::$app->cms->site); $rr->data = \Yii::$app->shop->shopFuser->toArray([], \Yii::$app->shop->shopFuser->extraFields()); return (array) $rr; } else { return $this->goBack(); } }