Commit 8c4b7f443bde3a8eec884a17ee24d5df2391009b

Authored by Yarik
1 parent 43b8c93c

Import browser beta v0.1

common/modules/product/models/Import.php
@@ -668,38 +668,38 @@ @@ -668,38 +668,38 @@
668 $taxGroup->is_filter = false; 668 $taxGroup->is_filter = false;
669 $taxGroup->save(); 669 $taxGroup->save();
670 } 670 }
671 -// $filters_options = explode(',', $filter[ 2 ][ 0 ]);  
672 -// foreach($filters_options as $filter_options) {  
673 -// $parsed_option_name = $this->parseName($filter_options);  
674 -// /**  
675 -// * @var TaxOption $option  
676 -// */  
677 -//  
678 -// if(!empty( $parsed_option_name[ 'remote_id' ] ) && ( $option = TaxOption::find()  
679 -// ->joinWith('lang')  
680 -// ->andFilterWhere([ 'remote_id' => $parsed_option_name[ 'remote_id' ] ])  
681 -// ->andFilterWhere([ 'tax_group_id' => $taxGroup->tax_group_id ])  
682 -// ->one() ) !== NULL  
683 -// ) {  
684 -// if(!empty( $option->lang )) {  
685 -// $option->lang->value = $parsed_option_name[ 'name' ];  
686 -// $option->lang->save();  
687 -// } else {  
688 -// throw new \Exception('Tax option with ID ' . $option->tax_option_id . ' and lang ' . Language::getCurrent()->language_id . ' doesn\'t exist');  
689 -// }  
690 -// } else {  
691 -// // Create option  
692 -// $option = new TaxOption();  
693 -// $option->generateLangs();  
694 -// $option_langs = $option->model_langs;  
695 -// foreach($option_langs as $option_lang) {  
696 -// $option_lang->value = $parsed_option_name[ 'name' ];  
697 -// }  
698 -// $option->tax_group_id = $taxGroup->tax_group_id;  
699 -// $option->save();  
700 -// }  
701 -// $options[] = $option->tax_option_id;  
702 -// } 671 + $filters_options = explode(',', $filter[ 2 ][ 0 ]);
  672 + foreach($filters_options as $filter_options) {
  673 + $parsed_option_name = $this->parseName($filter_options);
  674 + /**
  675 + * @var TaxOption $option
  676 + */
  677 +
  678 + if(!empty( $parsed_option_name[ 'remote_id' ] ) && ( $option = TaxOption::find()
  679 + ->joinWith('lang')
  680 + ->andFilterWhere([ 'remote_id' => $parsed_option_name[ 'remote_id' ] ])
  681 + ->andFilterWhere([ 'tax_group_id' => $taxGroup->tax_group_id ])
  682 + ->one() ) !== NULL
  683 + ) {
  684 + if(!empty( $option->lang )) {
  685 + $option->lang->value = $parsed_option_name[ 'name' ];
  686 + $option->lang->save();
  687 + } else {
  688 + throw new \Exception('Tax option with ID ' . $option->tax_option_id . ' and lang ' . Language::getCurrent()->language_id . ' doesn\'t exist');
  689 + }
  690 + } else {
  691 + // Create option
  692 + $option = new TaxOption();
  693 + $option->generateLangs();
  694 + $option_langs = $option->model_langs;
  695 + foreach($option_langs as $option_lang) {
  696 + $option_lang->value = $parsed_option_name[ 'name' ];
  697 + }
  698 + $option->tax_group_id = $taxGroup->tax_group_id;
  699 + $option->save();
  700 + }
  701 + $options[] = $option->tax_option_id;
  702 + }
703 } 703 }
704 return $options; 704 return $options;
705 } 705 }
common/modules/product/models/Product.php
@@ -345,53 +345,53 @@ @@ -345,53 +345,53 @@
345 ->sum('quantity'); 345 ->sum('quantity');
346 } 346 }
347 347
348 -// public function afterSave($insert, $changedAttributes)  
349 -// {  
350 -// parent::afterSave($insert, $changedAttributes);  
351 -//  
352 -//  
353 -// if(!empty($this->categories)){  
354 -// $categories = Category::findAll($this->categories);  
355 -// $this->unlinkAll('categories', true);  
356 -// foreach($categories as $category){  
357 -// $this->link('categories', $category);  
358 -// }  
359 -// }  
360 -//  
361 -// if(!empty($this->options)){  
362 -// $options = TaxOption::findAll($this->options);  
363 -// $this->unlinkAll('options',true);  
364 -// foreach($options as $option){  
365 -// $this->link('options', $option);  
366 -// }  
367 -// }  
368 -//  
369 -//  
370 -// if (!empty($this->_variants)) {  
371 -// $todel = [];  
372 -// foreach ($this->variants ?: [] as $_variant) {  
373 -// $todel[$_variant->product_variant_id] = $_variant->product_variant_id;  
374 -// }  
375 -// foreach ($this->_variants as $_variant) {  
376 -// if (!is_array($_variant)) {  
377 -// return;  
378 -// }  
379 -// if (!empty($_variant['product_variant_id'])) {  
380 -// unset($todel[$_variant['product_variant_id']]);  
381 -// $model = ProductVariant::findOne($_variant['product_variant_id']);  
382 -// } else {  
383 -// $model = new ProductVariant();  
384 -// }  
385 -// $_variant['product_id'] = $this->product_id;  
386 -// $model->load(['ProductVariant' => $_variant]);  
387 -// $model->product_id = $this->product_id;  
388 -// $model->save();  
389 -// }  
390 -// if (!empty($todel)) {  
391 -// ProductVariant::deleteAll(['product_variant_id' => $todel]);  
392 -// }  
393 -// }  
394 -// } 348 + public function afterSave($insert, $changedAttributes)
  349 + {
  350 + parent::afterSave($insert, $changedAttributes);
  351 +
  352 +
  353 + if(!empty($this->categories)){
  354 + $categories = Category::findAll($this->categories);
  355 + $this->unlinkAll('categories', true);
  356 + foreach($categories as $category){
  357 + $this->link('categories', $category);
  358 + }
  359 + }
  360 +
  361 + if(!empty($this->options)){
  362 + $options = TaxOption::findAll($this->options);
  363 + $this->unlinkAll('options',true);
  364 + foreach($options as $option){
  365 + $this->link('options', $option);
  366 + }
  367 + }
  368 +
  369 +
  370 + if (!empty($this->_variants)) {
  371 + $todel = [];
  372 + foreach ($this->variants ?: [] as $_variant) {
  373 + $todel[$_variant->product_variant_id] = $_variant->product_variant_id;
  374 + }
  375 + foreach ($this->_variants as $_variant) {
  376 + if (!is_array($_variant)) {
  377 + return;
  378 + }
  379 + if (!empty($_variant['product_variant_id'])) {
  380 + unset($todel[$_variant['product_variant_id']]);
  381 + $model = ProductVariant::findOne($_variant['product_variant_id']);
  382 + } else {
  383 + $model = new ProductVariant();
  384 + }
  385 + $_variant['product_id'] = $this->product_id;
  386 + $model->load(['ProductVariant' => $_variant]);
  387 + $model->product_id = $this->product_id;
  388 + $model->save();
  389 + }
  390 + if (!empty($todel)) {
  391 + ProductVariant::deleteAll(['product_variant_id' => $todel]);
  392 + }
  393 + }
  394 + }
395 395
396 public function imagesUpload() 396 public function imagesUpload()
397 { 397 {