protected function _updateShopProduct($product = [], ShopCmsContentElement $cmsContentElement) { $shopProduct = $cmsContentElement->shopProduct; if (!$shopProduct) { $shopProduct = new ShopProduct(); $shopProduct->id = $cmsContentElement->id; $shopProduct->save(); $this->stdout("\tadded ShopProduct\n", Console::FG_GREEN); } else { $this->stdout("\texist ShopProduct\n"); } $shopProduct->baseProductPriceValue = ArrayHelper::getValue($product, 'price'); $shopProduct->baseProductPriceCurrency = "RUB"; $shopProduct->purchasing_price = ArrayHelper::getValue($product, 'purchase_price'); $shopProduct->purchasing_currency = "RUB"; $shopProduct->quantity = (int) ArrayHelper::getValue($product, 'quantity'); if ($shopProduct->save()) { $this->stdout("\tShopProduct saved: quantity={$shopProduct->quantity}; price={$shopProduct->baseProductPriceValue}\n", Console::FG_GREEN); } else { $this->stdout("\tShopProduct not saved\n", Console::FG_RED); } }
public function update(AdminAction $adminAction) { /** * @var $model CmsContentElement */ $model = $this->model; $relatedModel = $model->relatedPropertiesModel; $shopProduct = ShopProduct::find()->where(['id' => $model->id])->one(); $productPrices = []; if (!$shopProduct) { $shopProduct = new ShopProduct(['id' => $model->id]); $shopProduct->save(); } else { if ($typePrices = ShopTypePrice::find()->where(['!=', 'def', Cms::BOOL_Y])->all()) { foreach ($typePrices as $typePrice) { $productPrice = ShopProductPrice::find()->where(['product_id' => $shopProduct->id, 'type_price_id' => $typePrice->id])->one(); if (!$productPrice) { $productPrice = new ShopProductPrice(['product_id' => $shopProduct->id, 'type_price_id' => $typePrice->id]); } if ($post = \Yii::$app->request->post()) { $data = ArrayHelper::getValue($post, 'prices.' . $typePrice->id); $productPrice->load($data, ""); } $productPrices[] = $productPrice; } } } $rr = new RequestResponse(); if (\Yii::$app->request->isAjax && !\Yii::$app->request->isPjax) { $model->load(\Yii::$app->request->post()); $relatedModel->load(\Yii::$app->request->post()); $shopProduct->load(\Yii::$app->request->post()); return \yii\widgets\ActiveForm::validateMultiple([$model, $relatedModel, $shopProduct]); } if ($rr->isRequestPjaxPost()) { $model->load(\Yii::$app->request->post()); $relatedModel->load(\Yii::$app->request->post()); $shopProduct->load(\Yii::$app->request->post()); /** * @var $productPrice ShopProductPrice */ foreach ($productPrices as $productPrice) { if ($productPrice->save()) { } else { \Yii::$app->getSession()->setFlash('error', \Yii::t('skeeks/shop/app', 'Check the correctness of the prices')); } } if ($model->save() && $relatedModel->save() && $shopProduct->save()) { \Yii::$app->getSession()->setFlash('success', \Yii::t('skeeks/shop/app', 'Saved')); if (\Yii::$app->request->post('submit-btn') == 'apply') { } else { return $this->redirect($this->indexUrl); } $model->refresh(); } else { $errors = []; if ($model->getErrors()) { foreach ($model->getErrors() as $error) { $errors[] = implode(', ', $error); } } \Yii::$app->getSession()->setFlash('error', \Yii::t('skeeks/shop/app', 'Could not save') . $errors); } } if (!$shopProduct->baseProductPrice) { $baseProductPrice = new ShopProductPrice(['type_price_id' => \Yii::$app->shop->baseTypePrice->id, 'currency_code' => \Yii::$app->money->currencyCode, 'product_id' => $model->id]); $baseProductPrice->save(); } return $this->render('_form', ['model' => $model, 'relatedModel' => $relatedModel, 'shopProduct' => $shopProduct, 'productPrices' => $productPrices, 'baseProductPrice' => $shopProduct->getBaseProductPrice()->one()]); }