diff --git a/Model/Config.php b/Model/Config.php index ddaf49fc..93e170e8 100644 --- a/Model/Config.php +++ b/Model/Config.php @@ -386,6 +386,37 @@ public function getFilterWhitelist(Store $store = null) return explode(',', $filterList) ?: []; } + /** + * @param Store|null $store + * @return array + */ + public function getFilterValuesWhitelist(Store $store = null): array + { + $filterList = $this->getStoreConfig('tweakwise/seo/filter_values_whitelist', $store); + + if (empty($filterList)) { + return []; + } + + $filterList = trim($filterList); + + $filterListExploded = explode(',', $filterList) ?: []; + if (empty($filterListExploded)) { + return []; + } + + $return = []; + foreach ($filterListExploded as $listItem) { + $item = explode('=', trim($listItem)) ?: null; + if ($item === null) { + continue; + } + $return[$item[0]][] = $item[1]; + } + + return $return; + } + /** * @param Store|null $store * @return int diff --git a/Model/Seo/FilterHelper.php b/Model/Seo/FilterHelper.php index ef9b2b5d..c1d2a678 100644 --- a/Model/Seo/FilterHelper.php +++ b/Model/Seo/FilterHelper.php @@ -12,6 +12,9 @@ use Emico\Tweakwise\Model\Client\Type\FacetType\SettingsType; use Emico\Tweakwise\Model\Config; use Magento\Catalog\Model\Layer\Resolver; +use Magento\Catalog\Model\Product; +use Magento\Eav\Api\AttributeRepositoryInterface; +use Magento\Framework\Exception\LocalizedException; class FilterHelper { @@ -41,7 +44,11 @@ class FilterHelper * @param Tweakwise $filterList * @param Config $config */ - public function __construct(Resolver $layerResolver, Tweakwise $filterList, Config $config) + public function __construct( + Resolver $layerResolver, + Tweakwise $filterList, + Config $config + ) { $this->layerResolver = $layerResolver; $this->tweakwiseFilterList = $filterList; @@ -62,7 +69,10 @@ public function shouldFilterBeIndexable(Item $item): bool return true; } - if (!$this->exceedsMaxAllowedFacets() && $this->isFilterItemInWhiteList($item)) { + if (!$this->exceedsMaxAllowedFacets() && + $this->isFilterItemInWhiteList($item) && + $this->isFilterValueItemInWhiteList($item) + ) { return true; } @@ -104,6 +114,15 @@ protected function getAttributeCodeFromFilterItem(Item $item) ->getUrlKey(); } + /** + * @param Item $item + * @return string|null + */ + protected function getAttributeValueFromFilterItem(Item $item): ?string + { + return $item->getAttribute()->getTitle(); + } + /** * @return bool */ @@ -132,6 +151,36 @@ protected function isFilterItemInWhiteList(Item $item): bool return \in_array($attributeCode, $filterWhiteList, true); } + /** + * @param Item $item + * @return bool + */ + protected function isFilterValueItemInWhiteList(Item $item): bool + { + $filterValuesWhiteList = $this->config->getFilterValuesWhitelist(); + $attributeValue = $this->getAttributeValueFromFilterItem($item); + + if (empty($filterValuesWhiteList)) { + return true; + } + + $attributeCode = $this->getAttributeCodeFromFilterItem($item); + + if (!array_key_exists($attributeCode, $filterValuesWhiteList)) { + return true; + } + + if ($attributeValue === null) { + return false; + } + + return \in_array( + strtolower($attributeValue), + array_map('strtolower', $filterValuesWhiteList[$attributeCode]), + true + ); + } + /** * @param bool $includeCategoryFilter * @return Item[] diff --git a/etc/adminhtml/system.xml b/etc/adminhtml/system.xml index 6282eea9..3500d86c 100644 --- a/etc/adminhtml/system.xml +++ b/etc/adminhtml/system.xml @@ -106,7 +106,18 @@ 1 - + + + + Attributes whose filter values should be indexable. + Add the filter and its value (as seen in navigator) comma separated. + For example: size=xs,size=s,size=m + + + 1 + + + Max allowed filters before rendering filters as not indexable validate-number diff --git a/etc/module.xml b/etc/module.xml index ee637537..c1534012 100644 --- a/etc/module.xml +++ b/etc/module.xml @@ -8,7 +8,7 @@ */ --> - +