Skip to content

Commit

Permalink
MAGETWO-66165: [GitHub][PR] Don't skip attribute options with a value…
Browse files Browse the repository at this point in the history
… of 0 from the layered navigation #7578
  • Loading branch information
magento-team authored Mar 16, 2017
2 parents a906767 + d11092a commit d906d6c
Showing 1 changed file with 60 additions and 23 deletions.
83 changes: 60 additions & 23 deletions app/code/Magento/CatalogSearch/Model/Layer/Filter/Attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
}

Expand All @@ -95,28 +96,64 @@ protected function _getItemsData()
$options = $attribute->getFrontend()
->getSelectOptions();
foreach ($options as $option) {
if (empty($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
*
* @param int|string $value
* @param array $optionsFacetedData
* @return int
*/
private function getOptionCount($value, $optionsFacetedData)
{
return isset($optionsFacetedData[$value]['count'])
? (int)$optionsFacetedData[$value]['count']
: 0;
}
}

0 comments on commit d906d6c

Please sign in to comment.