Commit 5c846dcb28ef59ae91e66953cdb0691d5ee27d71
1 parent
772a3ca4
-Default product's variant behavior
Showing
3 changed files
with
117 additions
and
1 deletions
Show diff stats
| 1 | +<?php | |
| 2 | + namespace common\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\ProductStock; | |
| 11 | + use common\modules\product\models\Stock; | |
| 12 | + | |
| 13 | + /** | |
| 14 | + * Class DefaultVariantBehavior | |
| 15 | + * | |
| 16 | + * @package common/behaviors | |
| 17 | + * @property Product $owner | |
| 18 | + * @see ProductVariant | |
| 19 | + */ | |
| 20 | + class DefaultVariantBehavior extends Behavior | |
| 21 | + { | |
| 22 | + | |
| 23 | + /** | |
| 24 | + * @todo add default variant image also | |
| 25 | + */ | |
| 26 | + | |
| 27 | + /** | |
| 28 | + * Catches product's insert event | |
| 29 | + * | |
| 30 | + * @return array | |
| 31 | + */ | |
| 32 | + public function events() | |
| 33 | + { | |
| 34 | + return [ | |
| 35 | + Product::EVENT_AFTER_INSERT => 'addDefaultVariant', | |
| 36 | + ]; | |
| 37 | + } | |
| 38 | + | |
| 39 | + /** | |
| 40 | + * Creates new default product's variant and sets it's to stock | |
| 41 | + * marked as default and sets to it unit also marked as default | |
| 42 | + */ | |
| 43 | + public function addDefaultVariant() | |
| 44 | + { | |
| 45 | + /** | |
| 46 | + * @var Stock $stock | |
| 47 | + * @var ProductUnit $defaultUnit | |
| 48 | + */ | |
| 49 | + $defaultVariant = new ProductVariant(); | |
| 50 | + $defaultVariant->product_id = $this->owner->id; | |
| 51 | + | |
| 52 | + /** | |
| 53 | + * Gets default unit for variant | |
| 54 | + */ | |
| 55 | + $defaultUnit = ProductUnit::find() | |
| 56 | + ->where( | |
| 57 | + [ | |
| 58 | + 'is_default' => true, | |
| 59 | + ] | |
| 60 | + ) | |
| 61 | + ->one(); | |
| 62 | + $defaultVariant->product_unit_id = $defaultUnit->id; | |
| 63 | + $defaultVariant->stock = 1; | |
| 64 | + | |
| 65 | + $defaultVariant->sku = 'default'; | |
| 66 | + $defaultVariant->remote_id = time(); | |
| 67 | + $defaultVariant->save(); | |
| 68 | + | |
| 69 | + /** | |
| 70 | + * Saving languages | |
| 71 | + */ | |
| 72 | + $activeLanguageIds = Language::find() | |
| 73 | + ->select('id') | |
| 74 | + ->where( | |
| 75 | + [ | |
| 76 | + 'status' => true, | |
| 77 | + ] | |
| 78 | + ) | |
| 79 | + ->asArray() | |
| 80 | + ->column(); | |
| 81 | + foreach ($activeLanguageIds as $languageId) { | |
| 82 | + $variantLanguage = new ProductVariantLang(); | |
| 83 | + $variantLanguage->language_id = $languageId; | |
| 84 | + $variantLanguage->product_variant_id = $defaultVariant->id; | |
| 85 | + $variantLanguage->title = 'default_' . $languageId; | |
| 86 | + $variantLanguage->save(); | |
| 87 | + } | |
| 88 | + /** | |
| 89 | + * Gets default stock | |
| 90 | + */ | |
| 91 | + $stock = Stock::find() | |
| 92 | + ->one(); | |
| 93 | + | |
| 94 | +// $image = ProductImage::find() | |
| 95 | +// ->where( | |
| 96 | +// [ | |
| 97 | +// 'product_id' => $this->owner->product_id, | |
| 98 | +// ] | |
| 99 | +// ) | |
| 100 | +// ->one(); | |
| 101 | +// $image->product_variant_id = $defaultVariant->product_variant_id; | |
| 102 | +// $image->save(); | |
| 103 | + | |
| 104 | + /** | |
| 105 | + * Add a new stock record | |
| 106 | + */ | |
| 107 | + $defaultStock = new ProductStock(); | |
| 108 | + $defaultStock->product_variant_id = $defaultVariant->id; | |
| 109 | + $defaultStock->stock_id = $stock->id; | |
| 110 | + $defaultStock->quantity = $defaultVariant->stock; | |
| 111 | + $defaultStock->save(); | |
| 112 | + } | |
| 113 | + | |
| 114 | + } | |
| 0 | 115 | \ No newline at end of file | ... | ... |
common/modules/product/models/Product.php
| ... | ... | @@ -2,6 +2,7 @@ |
| 2 | 2 | |
| 3 | 3 | namespace common\modules\product\models; |
| 4 | 4 | |
| 5 | + use common\behaviors\DefaultVariantBehavior; | |
| 5 | 6 | use common\behaviors\MultipleImgBehavior; |
| 6 | 7 | use common\behaviors\SaveMultipleFileBehavior; |
| 7 | 8 | use common\models\ProductToRating; |
| ... | ... | @@ -120,6 +121,7 @@ |
| 120 | 121 | 'language' => [ |
| 121 | 122 | 'class' => LanguageBehavior::className(), |
| 122 | 123 | ], |
| 124 | + 'defaultVariant' => DefaultVariantBehavior::className(), | |
| 123 | 125 | ]; |
| 124 | 126 | } |
| 125 | 127 | ... | ... |