From 5c846dcb28ef59ae91e66953cdb0691d5ee27d71 Mon Sep 17 00:00:00 2001 From: Alex Date: Mon, 7 Nov 2016 16:32:33 +0200 Subject: [PATCH] -Default product's variant behavior --- common/behaviors/DefaultVariantBehavior.php | 114 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ common/modules/product/models/Product.php | 2 ++ common/modules/product/views/manage/_form.php | 2 +- 3 files changed, 117 insertions(+), 1 deletion(-) create mode 100644 common/behaviors/DefaultVariantBehavior.php diff --git a/common/behaviors/DefaultVariantBehavior.php b/common/behaviors/DefaultVariantBehavior.php new file mode 100644 index 0000000..b29e8eb --- /dev/null +++ b/common/behaviors/DefaultVariantBehavior.php @@ -0,0 +1,114 @@ + 'addDefaultVariant', + ]; + } + + /** + * Creates new default product's variant and sets it's to stock + * marked as default and sets to it unit also marked as default + */ + public function addDefaultVariant() + { + /** + * @var Stock $stock + * @var ProductUnit $defaultUnit + */ + $defaultVariant = new ProductVariant(); + $defaultVariant->product_id = $this->owner->id; + + /** + * Gets default unit for variant + */ + $defaultUnit = ProductUnit::find() + ->where( + [ + 'is_default' => true, + ] + ) + ->one(); + $defaultVariant->product_unit_id = $defaultUnit->id; + $defaultVariant->stock = 1; + + $defaultVariant->sku = 'default'; + $defaultVariant->remote_id = time(); + $defaultVariant->save(); + + /** + * Saving languages + */ + $activeLanguageIds = Language::find() + ->select('id') + ->where( + [ + 'status' => true, + ] + ) + ->asArray() + ->column(); + foreach ($activeLanguageIds as $languageId) { + $variantLanguage = new ProductVariantLang(); + $variantLanguage->language_id = $languageId; + $variantLanguage->product_variant_id = $defaultVariant->id; + $variantLanguage->title = 'default_' . $languageId; + $variantLanguage->save(); + } + /** + * Gets default stock + */ + $stock = Stock::find() + ->one(); + +// $image = ProductImage::find() +// ->where( +// [ +// 'product_id' => $this->owner->product_id, +// ] +// ) +// ->one(); +// $image->product_variant_id = $defaultVariant->product_variant_id; +// $image->save(); + + /** + * Add a new stock record + */ + $defaultStock = new ProductStock(); + $defaultStock->product_variant_id = $defaultVariant->id; + $defaultStock->stock_id = $stock->id; + $defaultStock->quantity = $defaultVariant->stock; + $defaultStock->save(); + } + + } \ No newline at end of file diff --git a/common/modules/product/models/Product.php b/common/modules/product/models/Product.php index 1e58282..8e722ac 100755 --- a/common/modules/product/models/Product.php +++ b/common/modules/product/models/Product.php @@ -2,6 +2,7 @@ namespace common\modules\product\models; + use common\behaviors\DefaultVariantBehavior; use common\behaviors\MultipleImgBehavior; use common\behaviors\SaveMultipleFileBehavior; use common\models\ProductToRating; @@ -120,6 +121,7 @@ 'language' => [ 'class' => LanguageBehavior::className(), ], + 'defaultVariant' => DefaultVariantBehavior::className(), ]; } diff --git a/common/modules/product/views/manage/_form.php b/common/modules/product/views/manage/_form.php index efbab32..b80e0b6 100755 --- a/common/modules/product/views/manage/_form.php +++ b/common/modules/product/views/manage/_form.php @@ -112,7 +112,7 @@ ), [ 'multiple' => true, - 'unselect' => null, + 'unselect' => NULL, ] ) ->label($group->lang->title); -- libgit2 0.21.4