Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
namespace Demo\Navigation\Plugin\Layer;
use Magento\Catalog\Model\ResourceModel\Layer\Filter\Attribute;
use Magento\Framework\App\RequestInterface;
use Magento\Catalog\Model\Layer\Filter\ItemFactory;
use Magento\Catalog\Model\Category;
use Magento\Catalog\Model\Layer\FilterList;
use Magento\Catalog\Model\Product;
class FilterListPlugin
{
/**
* @var ItemFactory
*/
protected $itemFactory;
/**
* @var Attribute
*/
protected $attributeResource;
/**
* @var RequestInterface
*/
protected $requestInterface;
/**
* FilterListPlugin constructor.
* @param ItemFactory $itemFactory
* @param Attribute $attributeResource
* @param RequestInterface $requestInterface
*/
public function __construct(
ItemFactory $itemFactory,
Attribute $attributeResource,
RequestInterface $requestInterface
) {
$this->itemFactory = $itemFactory;
$this->attributeResource = $attributeResource;
$this->requestInterface = $requestInterface;
}
/**
* Only allow defined attributes as filters
* and sort them in the needed order
*
* @param FilterList $subject
* @param $result
* @return array
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function afterGetFilters(FilterList $subject, $result) //@codingStandardsIgnoreLine
{
$data = [
'cat' => Category::ENTITY,
'demo_product_type' => Product::ENTITY,
'demo_material' => Product::ENTITY,
'demo_packaging' => Product::ENTITY,
'demo_characteristics' => Product::ENTITY,
];
foreach ($result as $filter) {
if (in_array($filter->getRequestVar(), array_keys($data))) {
if ($data[$filter->getRequestVar()] === Category::ENTITY) {
$filter->setItems(
$this->getCategoryItems($filter)
);
}
if ($data[$filter->getRequestVar()] === Product::ENTITY) {
$filter->setItems(
$this->getAttributeItems($filter)
);
}
$data[$filter->getRequestVar()] = $filter;
}
}
return $data;
}
/**
* @param $filter
* @param $label
* @param $value
* @param int $count
* @param bool $active
* @return mixed
*/
protected function _createItem($filter, $label, $value, $count = 0, $active = false)
{
return $this->itemFactory->create()
->setFilter($filter)
->setLabel($label)
->setValue($value)
->setCount($count)
->setIsActive($active);
}
/**
* @param $filter
* @return array
*/
protected function getAttributeItems($filter)
{
$_data = [];
$attribute = $filter->getAttributeModel();
foreach ($attribute->getOptions() as $option) {
if (empty($option['value']) || !$attribute->getIsFilterable()) {
continue;
}
$options = $this->attributeResource->getCount($filter);
$count = isset($options[$option->getvalue()]) ? $options[$option->getvalue()] : 0;
if ($this->requestInterface->getParam($filter->getRequestVar()) == $option->getValue()) {
$active = true;
} else {
$active = false;
}
$_data[] = $this->_createItem(
$filter,
$option->getLabel(),
$option->getValue(),
$count,
$active
);
}
return $_data;
}
/**
* @param $filter
* @return array
*/
protected function getCategoryItems($filter)
{
$_data = [];
$category = $filter->getLayer()->getCurrentCategory();
if ($category->hasChildren()) {
foreach ($category->getChildrenCategories() as $child) {
if ($child->getProductCount() > 0 && $child->getIsActive()) {
if ($this->requestInterface->getParam($filter->getRequestVar())
&& $this->requestInterface->getParam($filter->getRequestVar()) == $child->getId()
) {
$active = true;
} else {
$active = false;
}
$_data[$child->getPosition()] = $this->_createItem(
$filter,
$child->getName(),
$child->getId(),
$child->getProductCount(),
$active
);
}
}
}
ksort($_data);
return $_data;
}
}
@mikehenze

This comment has been minimized.

Copy link

@mikehenze mikehenze commented Sep 28, 2017

Hi I would really like to use this in 2.2
But how can i install this?

@nilarocks

This comment has been minimized.

Copy link

@nilarocks nilarocks commented Nov 10, 2017

Where i add this?

@jajajaime

This comment has been minimized.

Copy link

@jajajaime jajajaime commented Dec 6, 2018

This worked great on Magento 2.2.6!
For those of you wondering, you have to add this as a plugin to Magento\Catalog\Model\Layer\FilterList on your frontend di.xml.

@RajuA3112

This comment has been minimized.

Copy link

@RajuA3112 RajuA3112 commented Jun 20, 2019

hello,
I'm implementing this in Magento 2.3, But it is not working, Can you help me or Can you give me any idea

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment