/** * Обновление цен и налчия товаров */ public function actionProductsUpdate() { $contentIds = (array) \Yii::$app->v3toysSettings->content_ids; if (!$contentIds) { $this->stdout("Не настроен v3toys комонент: {$total}\n", Console::FG_RED); return; } $count = ShopCmsContentElement::find()->where(['content_id' => $contentIds])->count(); $this->stdout("Всего товаров: {$count}\n", Console::BOLD); if ($count) { foreach (ShopCmsContentElement::find()->where(['content_id' => $contentIds])->each(10) as $element) { $this->stdout("\t{$element->id}: {$element->name}\n"); $v3id = $element->relatedPropertiesModel->getAttribute(\Yii::$app->v3toysSettings->v3toysIdPropertyName); if ($v3id) { $response = \Yii::$app->v3toysApi->getProductsDataByIds(['products_ids' => $v3id]); $this->stdout("\t\tОтвет получен из api\n"); if ($response->isOk) { if ($response->data) { $data = $response->data[0]; $priceFromApi = (double) ArrayHelper::getValue($data, 'price'); $quantityFromApi = (int) ArrayHelper::getValue($data, 'quantity'); $isChange = false; $ourPrice = $priceFromApi + $priceFromApi / 100 * \Yii::$app->v3toysSettings->price_discount_percent; $ourPrice = round($ourPrice); $discountValue = \Yii::$app->v3toysSettings->price_discount_percent; $guiding_buy_price = (double) ArrayHelper::getValue($data, 'buy_price'); $mr_price = (double) ArrayHelper::getValue($data, 'mr_price'); if ($ourPrice > $guiding_buy_price) { $this->stdout("\t\t{$priceFromApi} + {$discountValue}% = {$ourPrice}\n"); } else { $ourPrice = $priceFromApi; $this->stdout("\t\tНаша цена со скидкой {$ourPrice} < закупочной {$guiding_buy_price} оставим {$priceFromApi}\n"); } if ($ourPrice < $mr_price) { $ourPrice = $mr_price; $this->stdout("\t\t MR PRICE = {$mr_price}\n", Console::FG_YELLOW); } if ($ourPrice != $element->shopProduct->baseProductPriceValue) { $isChange = true; $this->stdout("\t\tЦена изменилась была {$element->shopProduct->baseProductPriceValue} стала {$ourPrice}\n", Console::FG_GREEN); $element->shopProduct->purchasing_price = ArrayHelper::getValue($data, 'buy_price'); $element->shopProduct->purchasing_currency = "RUB"; $element->shopProduct->baseProductPriceValue = $ourPrice; $element->shopProduct->baseProductPriceCurrency = "RUB"; } else { $this->stdout("\t\tЦена не менялась\n"); } if ((int) ArrayHelper::getValue($data, 'quantity') != (int) $element->shopProduct->quantity) { $isChange = true; $this->stdout("\t\tИзменилось количество {$element->shopProduct->quantity} стало {$quantityFromApi}\n", Console::FG_GREEN); $element->shopProduct->quantity = (int) ArrayHelper::getValue($data, 'quantity'); } else { $this->stdout("\t\tКоличество не изменилось\n"); } if ($isChange) { if ($element->shopProduct->save()) { $this->stdout("\tДанные сохранены\n", Console::FG_GREEN); } else { $error = Json::encode($element->shopProduct->errors); $this->stdout("\tДанные не сохранены {$error}\n", Console::FG_RED); } } } else { \Yii::error('Нет информации о товаре: ' . $element->id, self::className()); $this->stdout("\tInvalid api\n", Console::FG_RED); } } else { $this->stdout("\tApi response bad\n", Console::FG_RED); continue; } } else { $this->stdout("\t{$element->id}: {$element->name}\n", Console::FG_RED); continue; } } } }
/** * @param array $product * @return array|null|ShopCmsContentElement|\yii\db\ActiveRecord * @throws \skeeks\cms\relatedProperties\models\InvalidParamException */ protected function _getCmsContentElementProduct($product = []) { $cmsContent = ShopCmsContentElement::find()->joinWith('relatedElementProperties map')->joinWith('relatedElementProperties.property property')->andWhere(['property.code' => \Yii::$app->v3toysSettings->v3toysIdPropertyName])->andWhere(['map.value' => ArrayHelper::getValue($product, 'external_id')])->joinWith('cmsContent as ccontent')->andWhere(['ccontent.code' => 'product'])->one(); if (!$cmsContent) { $content = CmsContent::findOne(['code' => 'product']); //Иначе создадим $cmsContent = new ShopCmsContentElement(); //$cmsContent->tree_id = ImportStockSaleV2::ROOT_TREE_ID; //Каталог $cmsContent->content_id = CmsContent::findOne(['code' => 'product'])->id; //Тип контента $cmsContent->name = ArrayHelper::getValue($product, 'name'); if ($cmsContent->save()) { $cmsContent->relatedPropertiesModel->setAttribute(\Yii::$app->v3toysSettings->v3toysIdPropertyName, ArrayHelper::getValue($product, 'external_id')); $cmsContent->relatedPropertiesModel->save(false); } else { throw new Exception("Not created product: " . serialize($cmsContent->getFirstErrors())); } $this->stdout("\tCreated element {$cmsContent->id}\n"); return $cmsContent; } $this->stdout("\tExist element {$cmsContent->id}\n"); return $cmsContent; }