From 0c6e4decd8ea183842c63818cc3e42a4b999ab78 Mon Sep 17 00:00:00 2001 From: Diego Cabrejas Date: Sat, 26 Nov 2016 15:48:07 +0000 Subject: [PATCH 1/4] correct criteria for skipping attribute options from the layered navigation --- .../Magento/CatalogSearch/Model/Layer/Filter/Attribute.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/code/Magento/CatalogSearch/Model/Layer/Filter/Attribute.php b/app/code/Magento/CatalogSearch/Model/Layer/Filter/Attribute.php index 8c46242abb110..cfb363fe0dca1 100644 --- a/app/code/Magento/CatalogSearch/Model/Layer/Filter/Attribute.php +++ b/app/code/Magento/CatalogSearch/Model/Layer/Filter/Attribute.php @@ -53,7 +53,7 @@ public function __construct( public function apply(\Magento\Framework\App\RequestInterface $request) { $attributeValue = $request->getParam($this->_requestVar); - if (empty($attributeValue)) { + if (empty($attributeValue) && !is_numeric($attributeValue)) { return $this; } $attribute = $this->getAttributeModel(); @@ -95,7 +95,7 @@ protected function _getItemsData() $options = $attribute->getFrontend() ->getSelectOptions(); foreach ($options as $option) { - if (empty($option['value'])) { + if (empty($option['value']) && !is_numeric($option['value'])) { continue; } From 3d7ca126f01f23ce4d02725a0c57bedcfac630be Mon Sep 17 00:00:00 2001 From: Eugene Tulika Date: Wed, 15 Mar 2017 20:39:30 -0500 Subject: [PATCH 2/4] MAGETWO-66165: [GitHub][PR] Don't skip attribute options with a value of 0 from the layered navigation magento/magento2#7578 - reduced complexity of the method --- .../Model/Layer/Filter/Attribute.php | 80 +++++++++++++------ 1 file changed, 57 insertions(+), 23 deletions(-) diff --git a/app/code/Magento/CatalogSearch/Model/Layer/Filter/Attribute.php b/app/code/Magento/CatalogSearch/Model/Layer/Filter/Attribute.php index a5c2ef3f5d8a7..49082f39a3561 100644 --- a/app/code/Magento/CatalogSearch/Model/Layer/Filter/Attribute.php +++ b/app/code/Magento/CatalogSearch/Model/Layer/Filter/Attribute.php @@ -84,9 +84,10 @@ protected function _getItemsData() ->getProductCollection(); $optionsFacetedData = $productCollection->getFacetedData($attribute->getAttributeCode()); - if (count($optionsFacetedData) === 0 - && $this->getAttributeIsFilterable($attribute) !== static::ATTRIBUTE_OPTIONS_ONLY_WITH_RESULTS - ) { + $isAttributeFilterable = + $this->getAttributeIsFilterable($attribute) === static::ATTRIBUTE_OPTIONS_ONLY_WITH_RESULTS; + + if (count($optionsFacetedData) === 0 && $isAttributeFilterable) { return $this->itemDataBuilder->build(); } @@ -95,29 +96,62 @@ protected function _getItemsData() $options = $attribute->getFrontend() ->getSelectOptions(); foreach ($options as $option) { - if (empty($option['value']) && !is_numeric($option['value'])) { - continue; - } + $this->buildOptionData($option, $isAttributeFilterable, $optionsFacetedData, $productSize); + } - $value = $option['value']; + return $this->itemDataBuilder->build(); + } - $count = isset($optionsFacetedData[$value]['count']) - ? (int)$optionsFacetedData[$value]['count'] - : 0; - // Check filter type - if ( - $this->getAttributeIsFilterable($attribute) === static::ATTRIBUTE_OPTIONS_ONLY_WITH_RESULTS - && (!$this->isOptionReducesResults($count, $productSize) || $count === 0) - ) { - continue; - } - $this->itemDataBuilder->addItemData( - $this->tagFilter->filter($option['label']), - $value, - $count - ); + /** + * Build option data + * + * @param array $option + * @param boolean $isAttributeFilterable + * @param array $optionsFacetedData + * @param int $productSize + * @return void + */ + private function buildOptionData($option, $isAttributeFilterable, $optionsFacetedData, $productSize) + { + $value = $this->getOptionValue($option); + if ($value === false) { + return; + } + $count = $this->getOptionCount($value, $optionsFacetedData); + if ($isAttributeFilterable && (!$this->isOptionReducesResults($count, $productSize) || $count === 0)) { + return; } - return $this->itemDataBuilder->build(); + $this->itemDataBuilder->addItemData( + $this->tagFilter->filter($option['label']), + $value, + $count + ); + } + + /** + * Retrieve option value if it exists + * + * @param array $option + * @return bool|string + */ + private function getOptionValue($option) + { + if (empty($option['value']) && !is_numeric($option['value'])) { + return false; + } + return $option['value']; + } + + /** + * Retrieve count of the options + * + * @return int + */ + private function getOptionCount($value, $optionsFacetedData) + { + return isset($optionsFacetedData[$value]['count']) + ? (int)$optionsFacetedData[$value]['count'] + : 0; } } From bf3cec50b798e771b24a8bcb81d76cccd8ce153d Mon Sep 17 00:00:00 2001 From: Eugene Tulika Date: Wed, 15 Mar 2017 21:03:27 -0500 Subject: [PATCH 3/4] MAGETWO-66165: [GitHub][PR] Don't skip attribute options with a value of 0 from the layered navigation magento/magento2#7578 - fixed documentation --- app/code/Magento/CatalogSearch/Model/Layer/Filter/Attribute.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/code/Magento/CatalogSearch/Model/Layer/Filter/Attribute.php b/app/code/Magento/CatalogSearch/Model/Layer/Filter/Attribute.php index 49082f39a3561..dc58b6ea51ef4 100644 --- a/app/code/Magento/CatalogSearch/Model/Layer/Filter/Attribute.php +++ b/app/code/Magento/CatalogSearch/Model/Layer/Filter/Attribute.php @@ -146,6 +146,8 @@ private function getOptionValue($option) /** * Retrieve count of the options * + * @param int|string $value + * @param array $optionsFacetedData * @return int */ private function getOptionCount($value, $optionsFacetedData) From 3fce6bb6222441c962059210b2e4786cd4ba95f7 Mon Sep 17 00:00:00 2001 From: Eugene Tulika Date: Thu, 16 Mar 2017 10:23:10 -0500 Subject: [PATCH 4/4] MAGETWO-66165: [GitHub][PR] Don't skip attribute options with a value of 0 from the layered navigation magento/magento2#7578 - fixed incorrect condition --- app/code/Magento/CatalogSearch/Model/Layer/Filter/Attribute.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/code/Magento/CatalogSearch/Model/Layer/Filter/Attribute.php b/app/code/Magento/CatalogSearch/Model/Layer/Filter/Attribute.php index dc58b6ea51ef4..085135ef8af75 100644 --- a/app/code/Magento/CatalogSearch/Model/Layer/Filter/Attribute.php +++ b/app/code/Magento/CatalogSearch/Model/Layer/Filter/Attribute.php @@ -87,7 +87,7 @@ protected function _getItemsData() $isAttributeFilterable = $this->getAttributeIsFilterable($attribute) === static::ATTRIBUTE_OPTIONS_ONLY_WITH_RESULTS; - if (count($optionsFacetedData) === 0 && $isAttributeFilterable) { + if (count($optionsFacetedData) === 0 && !$isAttributeFilterable) { return $this->itemDataBuilder->build(); }