Skip to content

Commit 69648ad

Browse files
authored
ENGCOM-7087: [feature] Display category filter item in layered navigation based on the system configuration from admin area #27165
2 parents 5020cb6 + 0ca4e40 commit 69648ad

10 files changed

+367
-4
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Catalog\Model\Config;
9+
10+
use Magento\Framework\App\Config\ScopeConfigInterface;
11+
use Magento\Framework\Exception\NoSuchEntityException;
12+
use Magento\Store\Model\ScopeInterface;
13+
use Magento\Store\Model\StoreManagerInterface;
14+
15+
/**
16+
* Config for category in the layered navigation
17+
*/
18+
class LayerCategoryConfig
19+
{
20+
private const XML_PATH_CATALOG_LAYERED_NAVIGATION_DISPLAY_CATEGORY = 'catalog/layered_navigation/display_category';
21+
22+
/**
23+
* @var ScopeConfigInterface
24+
*/
25+
private $scopeConfig;
26+
27+
/**
28+
* @var StoreManagerInterface
29+
*/
30+
private $storeManager;
31+
32+
/**
33+
* LayerCategoryConfig constructor
34+
*
35+
* @param ScopeConfigInterface $scopeConfig
36+
* @param StoreManagerInterface $storeManager
37+
*/
38+
public function __construct(
39+
ScopeConfigInterface $scopeConfig,
40+
StoreManagerInterface $storeManager
41+
) {
42+
$this->scopeConfig = $scopeConfig;
43+
$this->storeManager = $storeManager;
44+
}
45+
46+
/**
47+
* Check if category filter item should be added in the layered navigation
48+
*
49+
* @param string $scopeType
50+
* @param null|int|string $scopeCode
51+
*
52+
* @return bool
53+
*/
54+
public function isCategoryFilterVisibleInLayerNavigation(
55+
$scopeType = ScopeInterface::SCOPE_STORES,
56+
$scopeCode = null
57+
): bool {
58+
if (!$scopeCode) {
59+
$scopeCode = $this->getStoreId();
60+
}
61+
62+
return $this->scopeConfig->isSetFlag(
63+
static::XML_PATH_CATALOG_LAYERED_NAVIGATION_DISPLAY_CATEGORY,
64+
$scopeType,
65+
$scopeCode
66+
);
67+
}
68+
69+
/**
70+
* Get the current store ID
71+
*
72+
* @return int
73+
*
74+
* @throws NoSuchEntityException
75+
*/
76+
private function getStoreId(): int
77+
{
78+
return (int) $this->storeManager->getStore()->getId();
79+
}
80+
}

app/code/Magento/Catalog/Model/Layer/FilterList.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77

88
namespace Magento\Catalog\Model\Layer;
99

10+
use Magento\Catalog\Model\Config\LayerCategoryConfig;
11+
use Magento\Framework\App\ObjectManager;
12+
1013
/**
1114
* Layer navigation filters
1215
*/
@@ -44,18 +47,26 @@ class FilterList
4447
*/
4548
protected $filters = [];
4649

50+
/**
51+
* @var LayerCategoryConfig
52+
*/
53+
private $layerCategoryConfig;
54+
4755
/**
4856
* @param \Magento\Framework\ObjectManagerInterface $objectManager
4957
* @param FilterableAttributeListInterface $filterableAttributes
58+
* @param LayerCategoryConfig $layerCategoryConfig
5059
* @param array $filters
5160
*/
5261
public function __construct(
5362
\Magento\Framework\ObjectManagerInterface $objectManager,
5463
FilterableAttributeListInterface $filterableAttributes,
64+
LayerCategoryConfig $layerCategoryConfig,
5565
array $filters = []
5666
) {
5767
$this->objectManager = $objectManager;
5868
$this->filterableAttributes = $filterableAttributes;
69+
$this->layerCategoryConfig = $layerCategoryConfig;
5970

6071
/** Override default filter type models */
6172
$this->filterTypes = array_merge($this->filterTypes, $filters);
@@ -70,9 +81,11 @@ public function __construct(
7081
public function getFilters(\Magento\Catalog\Model\Layer $layer)
7182
{
7283
if (!count($this->filters)) {
73-
$this->filters = [
74-
$this->objectManager->create($this->filterTypes[self::CATEGORY_FILTER], ['layer' => $layer]),
75-
];
84+
if ($this->layerCategoryConfig->isCategoryFilterVisibleInLayerNavigation()) {
85+
$this->filters = [
86+
$this->objectManager->create($this->filterTypes[self::CATEGORY_FILTER], ['layer' => $layer]),
87+
];
88+
}
7689
foreach ($this->filterableAttributes->getList() as $attribute) {
7790
$this->filters[] = $this->createAttributeFilter($attribute, $layer);
7891
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
-->
7+
8+
<actionGroups xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:mftf:Test/etc/actionGroupSchema.xsd">
9+
<!-- On a category page with layered navigation, verify if the category filter item is NOT present -->
10+
<actionGroup name="AssertStorefrontLayeredNavigationCategoryFilterNotVisibleActionGroup">
11+
<!-- Verify category filter item is NOT present -->
12+
<dontSee selector="{{StorefrontCategorySidebarSection.layeredFilterBlock}}" userInput="Category" stepKey="seeCategoryFilterInLayeredNav"/>
13+
</actionGroup>
14+
</actionGroups>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
-->
7+
8+
<actionGroups xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:mftf:Test/etc/actionGroupSchema.xsd">
9+
<!-- On a category page with layered navigation, verify if the category filter item is present -->
10+
<actionGroup name="AssertStorefrontLayeredNavigationCategoryFilterVisibleActionGroup">
11+
<!-- Verify category filter item is present -->
12+
<see selector="{{StorefrontCategorySidebarSection.layeredFilterBlock}}" userInput="Category" stepKey="seeCategoryFilterInLayeredNav"/>
13+
</actionGroup>
14+
</actionGroups>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
9+
<entities xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
10+
xsi:noNamespaceSchemaLocation="urn:magento:mftf:DataGenerator/etc/dataProfileSchema.xsd">
11+
<entity name="EnableCategoryFilterOnCategoryPageConfigData">
12+
<data key="path">catalog/layered_navigation/display_category</data>
13+
<data key="value">1</data>
14+
</entity>
15+
<entity name="DisableCategoryFilterOnCategoryPageConfigData">
16+
<data key="path">catalog/layered_navigation/display_category</data>
17+
<data key="value">0</data>
18+
</entity>
19+
</entities>
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
9+
<tests xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:mftf:Test/etc/testSchema.xsd">
10+
<test name="StorefrontCategoryPageWithCategoryFilterTest">
11+
<annotations>
12+
<title value="Category with Layered Navigation - verify presence of category filter"/>
13+
<stories value="Category page: Layered Navigation with category filter"/>
14+
<description value="Verify that the category filter is present in layered navigation on category page"/>
15+
<features value="Catalog"/>
16+
<severity value="MINOR"/>
17+
<group value="Catalog"/>
18+
</annotations>
19+
20+
<before>
21+
<!-- Create one category -->
22+
<createData entity="_defaultCategory" stepKey="defaultCategory">
23+
<field key="name">TopCategory</field>
24+
</createData>
25+
<!-- Create second category, having as parent the 1st one -->
26+
<createData entity="SubCategoryWithParent" stepKey="subCategory">
27+
<field key="name">SubCategory</field>
28+
<field key="parent_id">$$defaultCategory.id$$</field>
29+
<requiredEntity createDataKey="defaultCategory"/>
30+
</createData>
31+
32+
<!-- Create a product assigned to the 1st category -->
33+
<createData entity="_defaultProduct" stepKey="createSimpleProduct1">
34+
<requiredEntity createDataKey="defaultCategory"/>
35+
</createData>
36+
37+
<!-- Create 2nd product assigned to the 2nd category -->
38+
<!-- The "Category filter" item is not shown in layered navigation -->
39+
<!-- if there are not subcategories with products to show -->
40+
<createData entity="_defaultProduct" stepKey="createSimpleProduct2">
41+
<requiredEntity createDataKey="subCategory"/>
42+
</createData>
43+
44+
<!-- Set the category filter to be present on the category page layered navigation -->
45+
<magentoCLI command="config:set {{EnableCategoryFilterOnCategoryPageConfigData.path}} {{EnableCategoryFilterOnCategoryPageConfigData.value}}" stepKey="setCategoryFilterVisibleOnStorefront"/>
46+
47+
<magentoCLI command="cache:flush" stepKey="clearCache1"/>
48+
</before>
49+
50+
<after>
51+
<deleteData createDataKey="createSimpleProduct1" stepKey="deleteProduct1"/>
52+
<deleteData createDataKey="createSimpleProduct2" stepKey="deleteProduct2"/>
53+
<deleteData createDataKey="subCategory" stepKey="deleteSubCategory"/>
54+
<deleteData createDataKey="defaultCategory" stepKey="deleteCategoryMainCategory"/>
55+
</after>
56+
57+
<actionGroup ref="StorefrontNavigateCategoryPageActionGroup" stepKey="navigateToCategoryPage">
58+
<argument name="category" value="$$defaultCategory$$"/>
59+
</actionGroup>
60+
61+
<actionGroup ref="AssertStorefrontLayeredNavigationCategoryFilterVisibleActionGroup" stepKey="checkCategoryFilterIsPresent" />
62+
</test>
63+
</tests>
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
9+
<tests xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:mftf:Test/etc/testSchema.xsd">
10+
<test name="StorefrontCategoryPageWithoutCategoryFilterTest">
11+
<annotations>
12+
<title value="Category with Layered Navigation - verify absence of category filter"/>
13+
<stories value="Category page: Layered Navigation without category filter"/>
14+
<description value="Verify that the category filter is NOT present in layered navigation on category page"/>
15+
<features value="Catalog"/>
16+
<severity value="MINOR"/>
17+
<group value="Catalog"/>
18+
</annotations>
19+
20+
<before>
21+
<!-- Create one category -->
22+
<createData entity="_defaultCategory" stepKey="defaultCategory">
23+
<field key="name">TopCategory</field>
24+
</createData>
25+
<!-- Create second category, having as parent the 1st one -->
26+
<createData entity="SubCategoryWithParent" stepKey="subCategory">
27+
<field key="name">SubCategory</field>
28+
<field key="parent_id">$$defaultCategory.id$$</field>
29+
<requiredEntity createDataKey="defaultCategory"/>
30+
</createData>
31+
32+
<!-- Create a product assigned to the 1st category -->
33+
<createData entity="_defaultProduct" stepKey="createSimpleProduct1">
34+
<requiredEntity createDataKey="defaultCategory"/>
35+
</createData>
36+
37+
<!-- Create 2nd product assigned to the 2nd category -->
38+
<!-- The "Category filter" item is not shown in layered navigation -->
39+
<!-- if there are not subcategories with products to show -->
40+
<createData entity="_defaultProduct" stepKey="createSimpleProduct2">
41+
<requiredEntity createDataKey="subCategory"/>
42+
</createData>
43+
44+
<!-- Set the category filter to NOT be present on the category page layered navigation -->
45+
<magentoCLI command="config:set {{DisableCategoryFilterOnCategoryPageConfigData.path}} {{DisableCategoryFilterOnCategoryPageConfigData.value}}" stepKey="hideCategoryFilterOnStorefront"/>
46+
47+
<magentoCLI command="cache:flush" stepKey="clearCache"/>
48+
</before>
49+
50+
<after>
51+
<deleteData createDataKey="createSimpleProduct1" stepKey="deleteProduct1"/>
52+
<deleteData createDataKey="createSimpleProduct2" stepKey="deleteProduct2"/>
53+
<deleteData createDataKey="subCategory" stepKey="deleteSubCategory"/>
54+
<deleteData createDataKey="defaultCategory" stepKey="deleteCategoryMainCategory"/>
55+
</after>
56+
57+
<actionGroup ref="StorefrontNavigateCategoryPageActionGroup" stepKey="navigateToCategoryPage">
58+
<argument name="category" value="$$defaultCategory$$"/>
59+
</actionGroup>
60+
61+
<actionGroup ref="AssertStorefrontLayeredNavigationCategoryFilterNotVisibleActionGroup" stepKey="checkCategoryFilterIsNotPresent" />
62+
</test>
63+
</tests>

0 commit comments

Comments
 (0)