From 21ce7afa7afd9888deef5a8e995c9c39efb51321 Mon Sep 17 00:00:00 2001 From: Alex Date: Wed, 26 Oct 2016 12:03:28 +0300 Subject: [PATCH] -Fix when cannot unset all options from product or variant -Fix when product have no options its variants don't display during filtering by variant's options --- common/modules/product/controllers/ManageController.php | 3 +++ common/modules/product/controllers/VariantController.php | 1 + common/modules/product/helpers/ProductHelper.php | 12 ++++++++---- common/modules/product/models/Product.php | 60 +++++++++++------------------------------------------------- frontend/controllers/CatalogController.php | 2 +- frontend/models/ProductFrontendSearch.php | 1 + 6 files changed, 25 insertions(+), 54 deletions(-) diff --git a/common/modules/product/controllers/ManageController.php b/common/modules/product/controllers/ManageController.php index bb8a8c1..d074619 100755 --- a/common/modules/product/controllers/ManageController.php +++ b/common/modules/product/controllers/ManageController.php @@ -9,6 +9,7 @@ use Yii; use common\modules\product\models\Product; use common\modules\product\models\ProductSearch; + use yii\helpers\VarDumper; use yii\web\Controller; use yii\web\NotFoundHttpException; use yii\filters\VerbFilter; @@ -125,7 +126,9 @@ public function actionUpdate($id) { $model = $this->findModel($id); + if($model->load(Yii::$app->request->post())) { + $model->unlinkAll('options',true); $model->imagesUpload = UploadedFile::getInstances($model, 'imagesUpload'); if($model->save()) { if(!empty( $model->imagesUpload ) && ( ( $images = $model->imagesUpload() ) !== false )) { diff --git a/common/modules/product/controllers/VariantController.php b/common/modules/product/controllers/VariantController.php index 7e35e64..90ffb5c 100755 --- a/common/modules/product/controllers/VariantController.php +++ b/common/modules/product/controllers/VariantController.php @@ -182,6 +182,7 @@ class VariantController extends Controller { $model = $this->findModel($id); if ($model->load(Yii::$app->request->post())) { + $model->unlinkAll('options',true); if ($model->save()) { diff --git a/common/modules/product/helpers/ProductHelper.php b/common/modules/product/helpers/ProductHelper.php index 25c83c5..7826070 100755 --- a/common/modules/product/helpers/ProductHelper.php +++ b/common/modules/product/helpers/ProductHelper.php @@ -251,17 +251,21 @@ Product::tableName() . '.product_id IN ( SELECT DISTINCT products FROM ( - SELECT product_id AS products + SELECT product_id AS products FROM product + WHERE + product_id IN ( + SELECT product_id FROM product_option INNER JOIN tax_option ON tax_option.tax_option_id = product_option.option_id INNER JOIN tax_group ON tax_group.tax_group_id = tax_option.tax_group_id - WHERE tax_group.alias LIKE \''. $key .'\' AND tax_option.alias IN (\'' . implode('\',\'', $param) . '\') OR product_id IN ( - (SELECT product_id AS products + WHERE tax_group.alias LIKE \''. $key .'\' AND tax_option.alias IN (\'' . implode('\',\'', $param) . '\')) + OR product_id IN ( + SELECT product_id FROM product_variant_option INNER JOIN product_variant ON product_variant_option.product_variant_id = product_variant.product_variant_id INNER JOIN tax_option ON tax_option.tax_option_id = product_variant_option.option_id INNER JOIN tax_group ON tax_group.tax_group_id = tax_option.tax_group_id - WHERE tax_group.alias LIKE \''. $key .'\' AND tax_option.alias IN (\'' . implode('\',\'', $param) . '\')) + WHERE tax_group.alias LIKE \''. $key .'\' AND tax_option.alias IN (\'' . implode('\',\'', $param) . '\') ) ) AS table_name )' diff --git a/common/modules/product/models/Product.php b/common/modules/product/models/Product.php index b6799b5..464e76c 100755 --- a/common/modules/product/models/Product.php +++ b/common/modules/product/models/Product.php @@ -13,7 +13,8 @@ use yii\db\ActiveRecord; use yii\helpers\ArrayHelper; use common\behaviors\DefaultVariantBehavior; - + use yii\helpers\VarDumper; + /** * This is the model class for table "{{%product}}". * @property string $name @@ -388,51 +389,7 @@ ->sum('quantity'); } -// public function afterSave($insert, $changedAttributes) -// { -// parent::afterSave($insert, $changedAttributes); -// -// $this->unlinkAll('categories', true); -// $this->unlinkAll('options', true); -// -// if($this->_categories) { -// $categories = Category::findAll($this->_categories); -// foreach($categories as $category) { -// $this->link('categories', $category); -// } -// } -// $options = TaxOption::findAll($this->_options); -// -// 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); @@ -446,13 +403,18 @@ } } - if(!empty($this->options)){ - $options = TaxOption::findAll($this->options); - $this->unlinkAll('options',true); + + $options = TaxOption::findAll($this->options); +// VarDumper::dump($this->options, 10, true); +// die(); + $this->unlinkAll('options',true); + if(is_array($options)){ foreach($options as $option){ $this->link('options', $option); } } + + if (!empty($this->_variants)) { diff --git a/frontend/controllers/CatalogController.php b/frontend/controllers/CatalogController.php index 2ee03f9..b558094 100755 --- a/frontend/controllers/CatalogController.php +++ b/frontend/controllers/CatalogController.php @@ -111,7 +111,7 @@ class CatalogController extends \yii\web\Controller $productModel = new ProductFrontendSearch(); //$productQuery = $productModel->getSearchQuery($category, $params); $productProvider = $productModel->search($category, $params); - + $brandModel = new BrandSearch(); $brands = $brandModel->getBrands($category, $params) ->all(); diff --git a/frontend/models/ProductFrontendSearch.php b/frontend/models/ProductFrontendSearch.php index 5d3b160..585fcf8 100755 --- a/frontend/models/ProductFrontendSearch.php +++ b/frontend/models/ProductFrontendSearch.php @@ -98,6 +98,7 @@ class ProductFrontendSearch extends Product { /** @var ActiveQuery $query */ // $query = $category->getRelations('product_categories'); $query = $category->getBaccaraProducts(); + } else { $query = Product::find(); } -- libgit2 0.21.4