From 9d539ac3212432464422ad8194c3b2fc2ce28cee Mon Sep 17 00:00:00 2001 From: Karnovsky A Date: Tue, 21 Jun 2016 22:49:07 +0300 Subject: [PATCH] Brands and options count --- common/modules/product/CatalogUrlManager.php | 10 +++++++++- common/modules/product/models/Brand.php | 2 ++ common/modules/product/models/BrandSearch.php | 5 ++++- frontend/controllers/CatalogController.php | 1 + frontend/models/ProductFrontendSearch.php | 15 ++++++++++----- frontend/views/catalog/products.php | 4 ++-- 6 files changed, 28 insertions(+), 9 deletions(-) diff --git a/common/modules/product/CatalogUrlManager.php b/common/modules/product/CatalogUrlManager.php index 83e3631..68b754f 100755 --- a/common/modules/product/CatalogUrlManager.php +++ b/common/modules/product/CatalogUrlManager.php @@ -118,6 +118,12 @@ class CatalogUrlManager implements UrlRuleInterface { $this->setFilterUrl($params, $url); + foreach ($params as $key => $param) { + if (empty($params[$key])) { + unset($params[$key]); + } + } + if (!empty($params) && ($query = http_build_query($params)) !== '') { $url .= '?' . $query; } @@ -193,7 +199,9 @@ class CatalogUrlManager implements UrlRuleInterface { break; } } - $url .= 'filter:'. implode(';', $filter); + if (!empty($filter)) { + $url .= 'filter:'. implode(';', $filter); + } unset($params['filter']); } } diff --git a/common/modules/product/models/Brand.php b/common/modules/product/models/Brand.php index 3b1ad37..6836e94 100755 --- a/common/modules/product/models/Brand.php +++ b/common/modules/product/models/Brand.php @@ -28,6 +28,8 @@ class Brand extends \yii\db\ActiveRecord { public $imageUpload; + public $_items_count = 0; + public function behaviors() { return [ diff --git a/common/modules/product/models/BrandSearch.php b/common/modules/product/models/BrandSearch.php index 7662917..01e6f1e 100755 --- a/common/modules/product/models/BrandSearch.php +++ b/common/modules/product/models/BrandSearch.php @@ -97,8 +97,11 @@ class BrandSearch extends Brand ->innerJoin(Product::tableName(), Product::tableName() .'.brand_id='. Brand::tableName() .'.brand_id') ->innerJoin(ProductCategory::tableName(), ProductCategory::tableName() .'.product_id='. Product::tableName() .'.product_id') ->with(['brandName']); + $query->innerJoin('product_variant', 'product_variant.product_id = '. Product::tableName() .'.product_id'); + $query->where(['!=', 'product_variant.stock', 0]); + $query->groupBy(Product::tableName() .'.product_id'); if (!empty($category)) { - $query->where([ + $query->andWhere([ ProductCategory::tableName() .'.category_id' => $category->category_id ]); } diff --git a/frontend/controllers/CatalogController.php b/frontend/controllers/CatalogController.php index 93a1f6c..b8d847e 100755 --- a/frontend/controllers/CatalogController.php +++ b/frontend/controllers/CatalogController.php @@ -145,6 +145,7 @@ class CatalogController extends \yii\web\Controller 'brandModel' => $brandModel, 'brands' => $brands, 'filter' => $filter, + 'params' => $params, 'productModel' => $productModel, 'productProvider' => $productProvider, 'groups' => $groups, diff --git a/frontend/models/ProductFrontendSearch.php b/frontend/models/ProductFrontendSearch.php index b9867b7..7679eee 100755 --- a/frontend/models/ProductFrontendSearch.php +++ b/frontend/models/ProductFrontendSearch.php @@ -94,20 +94,25 @@ class ProductFrontendSearch extends Product { } public function optionsForCategory($category = null, $params = []) { -// , -// 'COUNT('. ProductOption::tableName() .'.product_id) AS _items_count' $query = TaxOption::find() ->select([ - TaxOption::tableName() .'.*' + TaxOption::tableName() .'.*', + 'COUNT('. ProductOption::tableName() .'.product_id) AS _items_count' ]) ->innerJoin(ProductOption::tableName(), ProductOption::tableName() .'.option_id='. TaxOption::tableName() .'.tax_option_id') ->innerJoin(ProductCategory::tableName(), ProductCategory::tableName() .'.product_id='. ProductOption::tableName() .'.product_id') ->innerJoin(TaxGroup::tableName(), TaxGroup::tableName() .'.tax_group_id='. TaxOption::tableName() .'.tax_group_id') - ->andWhere([TaxGroup::tableName() .'.is_filter' => true]); + ->where([TaxGroup::tableName() .'.is_filter' => true]); + + $query->leftJoin('product_variant', 'product_variant.product_id = '. ProductCategory::tableName() .'.product_id'); + $query->andWhere(['!=', 'product_variant.stock', 0]); + $query->groupBy(TaxOption::tableName() .'.tax_option_id'); +// $query->having(['>', 'COUNT(product_variant.product_variant_id)', 0]); + if (!empty($category)) { $query->andWhere([ProductCategory::tableName() .'.category_id' => $category->category_id]); } - $query->groupBy(TaxOption::tableName() .'.tax_option_id'); + $query->orderBy(TaxOption::tableName() .'.sort', SORT_ASC); $query->limit(null); if (!empty($params['prices'])) { diff --git a/frontend/views/catalog/products.php b/frontend/views/catalog/products.php index 6cc7ddd..e465e51 100755 --- a/frontend/views/catalog/products.php +++ b/frontend/views/catalog/products.php @@ -120,7 +120,7 @@ $this->registerJsFile(Yii::getAlias('@web/js/ion.rangeSlider.js'),[ ?>
  • onchange="document.location=''" /> - name?>getProducts()->count()?>)*/?> + name?> (_items_count?>)
  • @@ -138,7 +138,7 @@ $this->registerJsFile(Yii::getAlias('@web/js/ion.rangeSlider.js'),[ ?>
  • /> - ValueRenderHTML?>_items_count?>)*/?> + ValueRenderHTML?> (_items_count?>)
  • -- libgit2 0.21.4