diff --git a/common/modules/product/models/Import.php b/common/modules/product/models/Import.php index 021577a..0f13b59 100755 --- a/common/modules/product/models/Import.php +++ b/common/modules/product/models/Import.php @@ -668,38 +668,38 @@ $taxGroup->is_filter = false; $taxGroup->save(); } -// $filters_options = explode(',', $filter[ 2 ][ 0 ]); -// foreach($filters_options as $filter_options) { -// $parsed_option_name = $this->parseName($filter_options); -// /** -// * @var TaxOption $option -// */ -// -// if(!empty( $parsed_option_name[ 'remote_id' ] ) && ( $option = TaxOption::find() -// ->joinWith('lang') -// ->andFilterWhere([ 'remote_id' => $parsed_option_name[ 'remote_id' ] ]) -// ->andFilterWhere([ 'tax_group_id' => $taxGroup->tax_group_id ]) -// ->one() ) !== NULL -// ) { -// if(!empty( $option->lang )) { -// $option->lang->value = $parsed_option_name[ 'name' ]; -// $option->lang->save(); -// } else { -// throw new \Exception('Tax option with ID ' . $option->tax_option_id . ' and lang ' . Language::getCurrent()->language_id . ' doesn\'t exist'); -// } -// } else { -// // Create option -// $option = new TaxOption(); -// $option->generateLangs(); -// $option_langs = $option->model_langs; -// foreach($option_langs as $option_lang) { -// $option_lang->value = $parsed_option_name[ 'name' ]; -// } -// $option->tax_group_id = $taxGroup->tax_group_id; -// $option->save(); -// } -// $options[] = $option->tax_option_id; -// } + $filters_options = explode(',', $filter[ 2 ][ 0 ]); + foreach($filters_options as $filter_options) { + $parsed_option_name = $this->parseName($filter_options); + /** + * @var TaxOption $option + */ + + if(!empty( $parsed_option_name[ 'remote_id' ] ) && ( $option = TaxOption::find() + ->joinWith('lang') + ->andFilterWhere([ 'remote_id' => $parsed_option_name[ 'remote_id' ] ]) + ->andFilterWhere([ 'tax_group_id' => $taxGroup->tax_group_id ]) + ->one() ) !== NULL + ) { + if(!empty( $option->lang )) { + $option->lang->value = $parsed_option_name[ 'name' ]; + $option->lang->save(); + } else { + throw new \Exception('Tax option with ID ' . $option->tax_option_id . ' and lang ' . Language::getCurrent()->language_id . ' doesn\'t exist'); + } + } else { + // Create option + $option = new TaxOption(); + $option->generateLangs(); + $option_langs = $option->model_langs; + foreach($option_langs as $option_lang) { + $option_lang->value = $parsed_option_name[ 'name' ]; + } + $option->tax_group_id = $taxGroup->tax_group_id; + $option->save(); + } + $options[] = $option->tax_option_id; + } } return $options; } diff --git a/common/modules/product/models/Product.php b/common/modules/product/models/Product.php index 033188d..ca0a49d 100755 --- a/common/modules/product/models/Product.php +++ b/common/modules/product/models/Product.php @@ -345,53 +345,53 @@ ->sum('quantity'); } -// public function afterSave($insert, $changedAttributes) -// { -// parent::afterSave($insert, $changedAttributes); -// -// -// if(!empty($this->categories)){ -// $categories = Category::findAll($this->categories); -// $this->unlinkAll('categories', true); -// foreach($categories as $category){ -// $this->link('categories', $category); -// } -// } -// -// if(!empty($this->options)){ -// $options = TaxOption::findAll($this->options); -// $this->unlinkAll('options',true); -// foreach($options as $option){ -// $this->link('options', $option); -// } -// } -// -// -// if (!empty($this->_variants)) { -// $todel = []; -// foreach ($this->variants ?: [] as $_variant) { -// $todel[$_variant->product_variant_id] = $_variant->product_variant_id; -// } -// foreach ($this->_variants as $_variant) { -// if (!is_array($_variant)) { -// return; -// } -// if (!empty($_variant['product_variant_id'])) { -// unset($todel[$_variant['product_variant_id']]); -// $model = ProductVariant::findOne($_variant['product_variant_id']); -// } else { -// $model = new ProductVariant(); -// } -// $_variant['product_id'] = $this->product_id; -// $model->load(['ProductVariant' => $_variant]); -// $model->product_id = $this->product_id; -// $model->save(); -// } -// if (!empty($todel)) { -// ProductVariant::deleteAll(['product_variant_id' => $todel]); -// } -// } -// } + public function afterSave($insert, $changedAttributes) + { + parent::afterSave($insert, $changedAttributes); + + + if(!empty($this->categories)){ + $categories = Category::findAll($this->categories); + $this->unlinkAll('categories', true); + foreach($categories as $category){ + $this->link('categories', $category); + } + } + + if(!empty($this->options)){ + $options = TaxOption::findAll($this->options); + $this->unlinkAll('options',true); + foreach($options as $option){ + $this->link('options', $option); + } + } + + + if (!empty($this->_variants)) { + $todel = []; + foreach ($this->variants ?: [] as $_variant) { + $todel[$_variant->product_variant_id] = $_variant->product_variant_id; + } + foreach ($this->_variants as $_variant) { + if (!is_array($_variant)) { + return; + } + if (!empty($_variant['product_variant_id'])) { + unset($todel[$_variant['product_variant_id']]); + $model = ProductVariant::findOne($_variant['product_variant_id']); + } else { + $model = new ProductVariant(); + } + $_variant['product_id'] = $this->product_id; + $model->load(['ProductVariant' => $_variant]); + $model->product_id = $this->product_id; + $model->save(); + } + if (!empty($todel)) { + ProductVariant::deleteAll(['product_variant_id' => $todel]); + } + } + } public function imagesUpload() { -- libgit2 0.21.4