magento2 icon indicating copy to clipboard operation
magento2 copied to clipboard

Category Layered Filter Issue

Open kost08 opened this issue 11 years ago • 28 comments

When you search for something, using sample data for example, a 'bag' no category filters are displayed. That happens because category filters are only rendered if Category in $categories has no children categories. In Magento\CatalogSearch\Model\Layer\Filter\Category in ::_getItemsData() $category is Default Category and $categories is a collection of it's direct children categories. 1 But $optionsFacetedData is an array of Categories which are children of the $categories 2 When you check

...
foreach ($categories as $category) {
    if ($category->getIsActive()
        -> && isset($optionsFacetedData[$category->getId()]) <-
        && $this->isOptionReducesResults($optionsFacetedData[$category->getId()]['count'], $collectionSize)
) {
...

There is no match and filters aren't rendered.

kost08 avatar Oct 15 '15 16:10 kost08

why not using isotope filtering?

magebay99 avatar Oct 21 '15 10:10 magebay99

The GitHub issue tracker is intended for technical issues only. Please refer to the Community Forums or Magento Stack Exchange site for technical questions. In your case, the programming questions forum is likely the most appropriate. Feel free to reopen this issue if you think you have encountered a bug in Magento 2.

davidalger avatar Nov 10 '15 22:11 davidalger

I think it is a technical issue (bug) with M2. Categories in layered are only rendered if Category in has no children. Layered category filter only shows non-anchored sub-categories, all is_anchor ones are not displayed. Same with search results layered category. This happen with the demo sample data as well as our migrated store from 1.9.01 (2000 categories in 4 levels). in our 1.9.0.1 all categories and sub-categories are displayed fine in layered left nav.

psol7 avatar Feb 07 '16 13:02 psol7

I have same kind of issue available for category with subcategory have product then its not display inside layered navigation.

I have checked category with subcategory all have is_anchor equal yes.

Any update for this issue?

RakeshJesadiya avatar Apr 23 '16 11:04 RakeshJesadiya

Here's a patch that fixes the issue:

diff --git a/www/vendor/magento/module-catalog-search/Model/Adapter/Mysql/Plugin/Aggregation/Category/DataProvider.php b/www/vendor/magento/module-catalog-search/Model/Adapter/Mysql/Plugin/Aggregation/Category/DataProvider.php
index f54a60f..678226b 100644
--- a/www/vendor/magento/module-catalog-search/Model/Adapter/Mysql/Plugin/Aggregation/Category/DataProvider.php
+++ b/www/vendor/magento/module-catalog-search/Model/Adapter/Mysql/Plugin/Aggregation/Category/DataProvider.php
@@ -74,8 +74,8 @@ class DataProvider
             $derivedTable->from(
                 ['main_table' => $this->resource->getTableName('catalog_category_product_index')],
                 [
-                    'entity_id' => 'product_id',
                     'value' => 'category_id',
+                    'entity_id' => 'product_id',
                 ]
             )->where('main_table.store_id = ?', $currentScopeId);
             $derivedTable->joinInner(

The problem is the following. In Magento\CatalogSearch\Model\Adapter\Mysql\Aggregation\DataProvider in the execute-method the fetchAssoc method is used to convert the query result into an associative array. The fetchAssoc method however uses the first column for the keys of the associated array. For all attribute aggregations the first column is always the value column, as it should be. However for the category aggregation the first column for some reason is the entity_id column, which is not unique. The fix consists thus in making the value column the first column to be returned by the select.

ktruehl avatar Apr 29 '16 16:04 ktruehl

Right on ktruehl. Problem fixed. Thks.

psol7 avatar Apr 29 '16 17:04 psol7

Hi @ktruehl Thanks for the patch! I've created a bug for internal tracking purposes - MAGETWO-52436 . Closing the other github issue as a dup.

choukalos avatar Apr 29 '16 18:04 choukalos

Thanks @ktruehl, your fix works perfectly.

Krapulat avatar May 02 '16 12:05 Krapulat

Current issue has been fixed and delivered in the scope of 2.0.3 patch release. So, it's now available on github The fix is the same as @ktruehl provided above https://github.com/magento/magento2/blob/develop/app/code/Magento/CatalogSearch/Model/Adapter/Mysql/Plugin/Aggregation/Category/DataProvider.php

maghamed avatar May 04 '16 16:05 maghamed

This fix isn't in the 2.0.3 patch, it's not even in 2.0.5, but I can still see the fix in the dev branch - any reason it's not been included yet?

mwsanders0 avatar May 17 '16 15:05 mwsanders0

It didn't get to 2.0.3. Now it's going to be delivered in the scope of 2.0.7 Patch release

maghamed avatar May 17 '16 15:05 maghamed

I am having a similar issue. Not all the categories that appear in the navigation menu dropdown are appearing in the category section in the layered navigation. There doesn't seem to be anything related with what shows up and what doesn't show up. All categories are set as anchors. Some categories have sub categories and some don't. It doesn't seem to make a difference as to them showing up or not. There are enabled products in all categories/sub categories.

I have tried deleting and re-adding one of the categories that wasn't showing up in the layered navigation and it did show up but then went away again after adding some sub categories to it.

This is with Magento 2.0.7 CE.

Here is a screenshot showing what I mean.

mage2categorybug

ghost avatar Jun 07 '16 17:06 ghost

Thanks for the fix @ktruehl

w3prodigy avatar Jun 10 '16 20:06 w3prodigy

I'm experiencing crtsl's issue as well... getting Categories to show in the Category Layered Navigation seems very buggy... deleting-readding categories seems to fix it temporarily... but then as I work in a category adding sub cats or products it seems to get removed... sometimes... I'm trying to narrow down the behavior exactly...

Mage 2.0.7

blizam avatar Jun 13 '16 22:06 blizam

Yeah, when I create a New Category and add products to it via the Category Products tab then the layered navigation renders on the front end for that Category.

If I then add 1 Subcategory, assign it some products in the same manner... everything is fine.

As soon as I add a second subcategory, assign it some products, and save then the parent Category disappears from Layered Navigation all together.

I feel like this is perhaps offtopic here though as the OP was posting about Search... I'll open a new Issue

blizam avatar Jun 14 '16 00:06 blizam

https://github.com/magento/magento2/issues/5009

blizam avatar Jun 14 '16 01:06 blizam

Internal issue created - MAGETWO-54383

maghamed avatar Jun 14 '16 14:06 maghamed

Hello @kost08, this issue has been fixed in the 2.1.0 Release, that's why I'm closing it. If you any questions or additional information regarding the issue feel free to reopen it or create a new one.

KrystynaKabannyk avatar Jun 23 '16 16:06 KrystynaKabannyk

Thank you all, its helped me lot.

@kost08 thanks for the post.

vivek-ted avatar Oct 18 '16 13:10 vivek-ted

I'm experiencing the exact same issue on Magento 2.1.2 on blank theme. Category filter disappear as soon as I add products on subcategory.

pitangaweb avatar Oct 24 '16 15:10 pitangaweb