Created
September 14, 2017 06:26
-
-
Save mhauri/93bc9ffe87250a55d5bb7eda4a85cd40 to your computer and use it in GitHub Desktop.
FilterListPlugin - https://magento.stackexchange.com/questions/111781/how-to-keep-all-filters-visible-in-magento-2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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