Commit db09c8fd2d7ebf1861d19997d4edb65e6dee5447
1 parent
e1d67a8e
Default variant
Showing
3 changed files
with
94 additions
and
1 deletions
Show diff stats
common/modules/product/behaviors/DefaultVariantBehavior.php
0 → 100755
| 1 | +<?php | ||
| 2 | + namespace common\modules\product\behaviors; | ||
| 3 | + | ||
| 4 | + use common\modules\language\models\Language; | ||
| 5 | + use common\modules\product\models\ProductVariantLang; | ||
| 6 | + use yii\base\Behavior; | ||
| 7 | + use common\modules\product\models\Product; | ||
| 8 | + use common\modules\product\models\ProductVariant; | ||
| 9 | + use common\modules\product\models\ProductUnit; | ||
| 10 | + use common\modules\product\models\Stock; | ||
| 11 | + | ||
| 12 | + /** | ||
| 13 | + * Class DefaultVariantBehavior | ||
| 14 | + * | ||
| 15 | + * @property Product $owner | ||
| 16 | + * @see ProductVariant | ||
| 17 | + */ | ||
| 18 | + class DefaultVariantBehavior extends Behavior | ||
| 19 | + { | ||
| 20 | + | ||
| 21 | + /** | ||
| 22 | + * @todo add default variant image also | ||
| 23 | + */ | ||
| 24 | + | ||
| 25 | + /** | ||
| 26 | + * Catches product's insert event | ||
| 27 | + * | ||
| 28 | + * @return array | ||
| 29 | + */ | ||
| 30 | + public function events() | ||
| 31 | + { | ||
| 32 | + return [ | ||
| 33 | + Product::EVENT_AFTER_INSERT => 'addDefaultVariant', | ||
| 34 | + ]; | ||
| 35 | + } | ||
| 36 | + | ||
| 37 | + /** | ||
| 38 | + * Creates new default product's variant and sets it's to stock | ||
| 39 | + * marked as default and sets to it unit also marked as default | ||
| 40 | + */ | ||
| 41 | + public function addDefaultVariant() | ||
| 42 | + { | ||
| 43 | + /** | ||
| 44 | + * @var Stock $stock | ||
| 45 | + * @var ProductUnit $defaultUnit | ||
| 46 | + */ | ||
| 47 | + $defaultVariant = new ProductVariant(); | ||
| 48 | + $defaultVariant->product_id = $this->owner->product_id; | ||
| 49 | + | ||
| 50 | + /** | ||
| 51 | + * Gets default unit for variant | ||
| 52 | + */ | ||
| 53 | + $defaultUnit = ProductUnit::find() | ||
| 54 | + ->where( | ||
| 55 | + [ | ||
| 56 | + 'is_default' => true, | ||
| 57 | + ] | ||
| 58 | + ) | ||
| 59 | + ->one(); | ||
| 60 | + $defaultVariant->product_unit_id = $defaultUnit->product_unit_id; | ||
| 61 | + $defaultVariant->stock = 1; | ||
| 62 | + | ||
| 63 | + $defaultVariant->sku = 'default'; | ||
| 64 | + $defaultVariant->remote_id = time(); | ||
| 65 | + $defaultVariant->save(); | ||
| 66 | + | ||
| 67 | + /** | ||
| 68 | + * Saving languages | ||
| 69 | + */ | ||
| 70 | + $activeLanguageIds = Language::find() | ||
| 71 | + ->select('language_id') | ||
| 72 | + ->where( | ||
| 73 | + [ | ||
| 74 | + 'status' => true, | ||
| 75 | + ] | ||
| 76 | + ) | ||
| 77 | + ->asArray() | ||
| 78 | + ->column(); | ||
| 79 | + foreach ($activeLanguageIds as $languageId) { | ||
| 80 | + $variantLanguage = new ProductVariantLang(); | ||
| 81 | + $variantLanguage->language_id = $languageId; | ||
| 82 | + $variantLanguage->product_variant_id = $defaultVariant->product_variant_id; | ||
| 83 | + $variantLanguage->name = 'default_' . $languageId; | ||
| 84 | + $variantLanguage->save(); | ||
| 85 | + } | ||
| 86 | + } | ||
| 87 | + | ||
| 88 | + } | ||
| 89 | + | ||
| 0 | \ No newline at end of file | 90 | \ No newline at end of file |
common/modules/product/models/Product.php
| @@ -10,6 +10,7 @@ | @@ -10,6 +10,7 @@ | ||
| 10 | use common\models\ProductToRating; | 10 | use common\models\ProductToRating; |
| 11 | use common\modules\comment\models\CommentModel; | 11 | use common\modules\comment\models\CommentModel; |
| 12 | use common\modules\language\behaviors\LanguageBehavior; | 12 | use common\modules\language\behaviors\LanguageBehavior; |
| 13 | + use common\modules\product\behaviors\DefaultVariantBehavior; | ||
| 13 | use common\modules\product\behaviors\FilterBehavior; | 14 | use common\modules\product\behaviors\FilterBehavior; |
| 14 | use common\modules\rubrication\models\TaxGroup; | 15 | use common\modules\rubrication\models\TaxGroup; |
| 15 | use common\modules\rubrication\models\TaxOption; | 16 | use common\modules\rubrication\models\TaxOption; |
| @@ -128,6 +129,9 @@ | @@ -128,6 +129,9 @@ | ||
| 128 | 'techSpec' => [ | 129 | 'techSpec' => [ |
| 129 | 'class' => TechBehavior::className(), | 130 | 'class' => TechBehavior::className(), |
| 130 | ], | 131 | ], |
| 132 | + 'defaultVariant' => [ | ||
| 133 | + 'class' => DefaultVariantBehavior::className(), | ||
| 134 | + ], | ||
| 131 | ]; | 135 | ]; |
| 132 | } | 136 | } |
| 133 | 137 |
common/modules/product/models/ProductStock.php
| @@ -70,7 +70,7 @@ class ProductStock extends \yii\db\ActiveRecord | @@ -70,7 +70,7 @@ class ProductStock extends \yii\db\ActiveRecord | ||
| 70 | 70 | ||
| 71 | 71 | ||
| 72 | public function getName(){ | 72 | public function getName(){ |
| 73 | - return (!empty($this->stock))? $this->stock->name : ''; | 73 | + return (!empty($this->stock))? $this->stock->lang->name : ''; |
| 74 | } | 74 | } |
| 75 | 75 | ||
| 76 | public function setName($value){ | 76 | public function setName($value){ |