Commit 9d539ac3212432464422ad8194c3b2fc2ce28cee

Authored by Karnovsky A
1 parent 433709ce

Brands and options count

common/modules/product/CatalogUrlManager.php
@@ -118,6 +118,12 @@ class CatalogUrlManager implements UrlRuleInterface { @@ -118,6 +118,12 @@ class CatalogUrlManager implements UrlRuleInterface {
118 118
119 $this->setFilterUrl($params, $url); 119 $this->setFilterUrl($params, $url);
120 120
  121 + foreach ($params as $key => $param) {
  122 + if (empty($params[$key])) {
  123 + unset($params[$key]);
  124 + }
  125 + }
  126 +
121 if (!empty($params) && ($query = http_build_query($params)) !== '') { 127 if (!empty($params) && ($query = http_build_query($params)) !== '') {
122 $url .= '?' . $query; 128 $url .= '?' . $query;
123 } 129 }
@@ -193,7 +199,9 @@ class CatalogUrlManager implements UrlRuleInterface { @@ -193,7 +199,9 @@ class CatalogUrlManager implements UrlRuleInterface {
193 break; 199 break;
194 } 200 }
195 } 201 }
196 - $url .= 'filter:'. implode(';', $filter); 202 + if (!empty($filter)) {
  203 + $url .= 'filter:'. implode(';', $filter);
  204 + }
197 unset($params['filter']); 205 unset($params['filter']);
198 } 206 }
199 } 207 }
common/modules/product/models/Brand.php
@@ -28,6 +28,8 @@ class Brand extends \yii\db\ActiveRecord @@ -28,6 +28,8 @@ class Brand extends \yii\db\ActiveRecord
28 { 28 {
29 public $imageUpload; 29 public $imageUpload;
30 30
  31 + public $_items_count = 0;
  32 +
31 public function behaviors() 33 public function behaviors()
32 { 34 {
33 return [ 35 return [
common/modules/product/models/BrandSearch.php
@@ -97,8 +97,11 @@ class BrandSearch extends Brand @@ -97,8 +97,11 @@ class BrandSearch extends Brand
97 ->innerJoin(Product::tableName(), Product::tableName() .'.brand_id='. Brand::tableName() .'.brand_id') 97 ->innerJoin(Product::tableName(), Product::tableName() .'.brand_id='. Brand::tableName() .'.brand_id')
98 ->innerJoin(ProductCategory::tableName(), ProductCategory::tableName() .'.product_id='. Product::tableName() .'.product_id') 98 ->innerJoin(ProductCategory::tableName(), ProductCategory::tableName() .'.product_id='. Product::tableName() .'.product_id')
99 ->with(['brandName']); 99 ->with(['brandName']);
  100 + $query->innerJoin('product_variant', 'product_variant.product_id = '. Product::tableName() .'.product_id');
  101 + $query->where(['!=', 'product_variant.stock', 0]);
  102 + $query->groupBy(Product::tableName() .'.product_id');
100 if (!empty($category)) { 103 if (!empty($category)) {
101 - $query->where([ 104 + $query->andWhere([
102 ProductCategory::tableName() .'.category_id' => $category->category_id 105 ProductCategory::tableName() .'.category_id' => $category->category_id
103 ]); 106 ]);
104 } 107 }
frontend/controllers/CatalogController.php
@@ -145,6 +145,7 @@ class CatalogController extends \yii\web\Controller @@ -145,6 +145,7 @@ class CatalogController extends \yii\web\Controller
145 'brandModel' => $brandModel, 145 'brandModel' => $brandModel,
146 'brands' => $brands, 146 'brands' => $brands,
147 'filter' => $filter, 147 'filter' => $filter,
  148 + 'params' => $params,
148 'productModel' => $productModel, 149 'productModel' => $productModel,
149 'productProvider' => $productProvider, 150 'productProvider' => $productProvider,
150 'groups' => $groups, 151 'groups' => $groups,
frontend/models/ProductFrontendSearch.php
@@ -94,20 +94,25 @@ class ProductFrontendSearch extends Product { @@ -94,20 +94,25 @@ class ProductFrontendSearch extends Product {
94 } 94 }
95 95
96 public function optionsForCategory($category = null, $params = []) { 96 public function optionsForCategory($category = null, $params = []) {
97 -// ,  
98 -// 'COUNT('. ProductOption::tableName() .'.product_id) AS _items_count'  
99 $query = TaxOption::find() 97 $query = TaxOption::find()
100 ->select([ 98 ->select([
101 - TaxOption::tableName() .'.*' 99 + TaxOption::tableName() .'.*',
  100 + 'COUNT('. ProductOption::tableName() .'.product_id) AS _items_count'
102 ]) 101 ])
103 ->innerJoin(ProductOption::tableName(), ProductOption::tableName() .'.option_id='. TaxOption::tableName() .'.tax_option_id') 102 ->innerJoin(ProductOption::tableName(), ProductOption::tableName() .'.option_id='. TaxOption::tableName() .'.tax_option_id')
104 ->innerJoin(ProductCategory::tableName(), ProductCategory::tableName() .'.product_id='. ProductOption::tableName() .'.product_id') 103 ->innerJoin(ProductCategory::tableName(), ProductCategory::tableName() .'.product_id='. ProductOption::tableName() .'.product_id')
105 ->innerJoin(TaxGroup::tableName(), TaxGroup::tableName() .'.tax_group_id='. TaxOption::tableName() .'.tax_group_id') 104 ->innerJoin(TaxGroup::tableName(), TaxGroup::tableName() .'.tax_group_id='. TaxOption::tableName() .'.tax_group_id')
106 - ->andWhere([TaxGroup::tableName() .'.is_filter' => true]); 105 + ->where([TaxGroup::tableName() .'.is_filter' => true]);
  106 +
  107 + $query->leftJoin('product_variant', 'product_variant.product_id = '. ProductCategory::tableName() .'.product_id');
  108 + $query->andWhere(['!=', 'product_variant.stock', 0]);
  109 + $query->groupBy(TaxOption::tableName() .'.tax_option_id');
  110 +// $query->having(['>', 'COUNT(product_variant.product_variant_id)', 0]);
  111 +
107 if (!empty($category)) { 112 if (!empty($category)) {
108 $query->andWhere([ProductCategory::tableName() .'.category_id' => $category->category_id]); 113 $query->andWhere([ProductCategory::tableName() .'.category_id' => $category->category_id]);
109 } 114 }
110 - $query->groupBy(TaxOption::tableName() .'.tax_option_id'); 115 +
111 $query->orderBy(TaxOption::tableName() .'.sort', SORT_ASC); 116 $query->orderBy(TaxOption::tableName() .'.sort', SORT_ASC);
112 $query->limit(null); 117 $query->limit(null);
113 if (!empty($params['prices'])) { 118 if (!empty($params['prices'])) {
frontend/views/catalog/products.php
@@ -120,7 +120,7 @@ $this->registerJsFile(Yii::getAlias('@web/js/ion.rangeSlider.js'),[ @@ -120,7 +120,7 @@ $this->registerJsFile(Yii::getAlias('@web/js/ion.rangeSlider.js'),[
120 ?> 120 ?>
121 <li> 121 <li>
122 <input type="checkbox" class="brands-option" <?= $checked ? ' checked' : ''?> onchange="document.location='<?= $option_url?>'" /> 122 <input type="checkbox" class="brands-option" <?= $checked ? ' checked' : ''?> onchange="document.location='<?= $option_url?>'" />
123 - <a href="<?= $option_url?>"><?= $brand->name?><?php /* (<?= $brand->getProducts()->count()?>)*/?></a> 123 + <a href="<?= $option_url?>"><?= $brand->name?>&nbsp;(<?= $brand->_items_count?>)</a>
124 </li> 124 </li>
125 <?php endforeach?> 125 <?php endforeach?>
126 </ul> 126 </ul>
@@ -138,7 +138,7 @@ $this-&gt;registerJsFile(Yii::getAlias(&#39;@web/js/ion.rangeSlider.js&#39;),[ @@ -138,7 +138,7 @@ $this-&gt;registerJsFile(Yii::getAlias(&#39;@web/js/ion.rangeSlider.js&#39;),[
138 ?> 138 ?>
139 <li> 139 <li>
140 <input type="checkbox" class="features-option" onchange="document.location='<?= $option_url?>'" <?php /* name="option[<?= $group->alias?>][]"value="<?= $option->alias?>"*/?><?= $checked ? ' checked' : ''?> /> 140 <input type="checkbox" class="features-option" onchange="document.location='<?= $option_url?>'" <?php /* name="option[<?= $group->alias?>][]"value="<?= $option->alias?>"*/?><?= $checked ? ' checked' : ''?> />
141 - <a href="<?= $option_url?>"><?= $option->ValueRenderHTML?><?php /*&nbsp;(<?= $option->_items_count?>)*/?></a> 141 + <a href="<?= $option_url?>"><?= $option->ValueRenderHTML?>&nbsp;(<?= $option->_items_count?>)</a>
142 </li> 142 </li>
143 <?php endforeach?> 143 <?php endforeach?>
144 </ul> 144 </ul>