Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace VendorFilterPreferenceModelLayerFilter;
- use MagentoFrameworkStdlibCookieCookieMetadataFactory;
- use MagentoFrameworkStdlibCookieManagerInterface;
- /**
- * Layer attribute filter
- */
- class Attribute extends MagentoCatalogSearchModelLayerFilterAttribute {
- const COOKIE_NAME = 'appliedfilter';
- const COOKIE_DURATION = 86400; // One day (86400 seconds)
- /**
- * @param MagentoCatalogModelLayerFilterItemFactory $filterItemFactory
- * @param MagentoStoreModelStoreManagerInterface $storeManager
- * @param MagentoCatalogModelLayer $layer
- * @param MagentoCatalogModelLayerFilterItemDataBuilder $itemDataBuilder
- * @param MagentoFrameworkFilterStripTags $tagFilter
- * @param MagentoFrameworkStdlibCookieManagerInterface $cookieManager
- * @param MagentoFrameworkStdlibCookieCookieMetadataFactory $cookieMetadataFactory
- * @param array $data
- */
- public function __construct(
- MagentoCatalogModelLayerFilterItemFactory $filterItemFactory, MagentoStoreModelStoreManagerInterface $storeManager, MagentoCatalogModelLayer $layer, MagentoCatalogModelLayerFilterItemDataBuilder $itemDataBuilder, MagentoFrameworkFilterStripTags $tagFilter, CookieManagerInterface $cookieManager, CookieMetadataFactory $cookieMetadataFactory, array $data = []
- ) {
- parent::__construct(
- $filterItemFactory, $storeManager, $layer, $itemDataBuilder, $tagFilter, $data
- );
- $this->_cookieManager = $cookieManager;
- $this->_cookieMetadataFactory = $cookieMetadataFactory;
- }
- /**
- * Apply attribute option filter to product collection
- *
- * @param MagentoFrameworkAppRequestInterface $request
- * @return $this
- * @throws MagentoFrameworkExceptionLocalizedException
- */
- public function apply(MagentoFrameworkAppRequestInterface $request) {
- $attributeValue = $request->getParam($this->_requestVar);
- if (empty($attributeValue))
- $cookieValue = '';
- $cookieValue = $this->_cookieManager->getCookie(self::COOKIE_NAME);
- if ($cookieValue) {
- $param = explode('-', $cookieValue);
- if ($this->_requestVar == $param[0]) {
- $attributeValue = $param[1];
- }
- }
- if (empty($attributeValue) && !is_numeric($attributeValue)) {
- return $this;
- }
- $attribute = $this->getAttributeModel();
- /** @var MagentoCatalogSearchModelResourceModelFulltextCollection $productCollection */
- $productCollection = $this->getLayer()
- ->getProductCollection();
- /* Set Cookie of Filter---Start */
- $cookieValue = $this->_cookieManager->getCookie(self::COOKIE_NAME);
- if (!$cookieValue || $cookieValue != $attribute->getAttributeCode() . '-' . $attributeValue) {
- $metadata = $this->_cookieMetadataFactory
- ->createPublicCookieMetadata()
- ->setDuration(self::COOKIE_DURATION);
- $this->_cookieManager
- ->setPublicCookie(self::COOKIE_NAME, $attribute->getAttributeCode() . '-' . $attributeValue, $metadata);
- }
- /* Set Cookie of Filter---End */
- $productCollection->addFieldToFilter($attribute->getAttributeCode(), $attributeValue);
- $label = $this->getOptionText($attributeValue);
- $this->getLayer()
- ->getState()
- ->addFilter($this->_createItem($label, $attributeValue));
- $this->setItems([]); // set items to disable show filtering
- return $this;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement