1

I have created a plugin to for the layered naviagtion filters. Fore this I need to save the filter chosen in the cookies but the cookies are now saving. When I disable the full page caching, then everything works fine. Please help.

Vendor/FilterPreference/Model/Layer/Filter/Attribute.php

<?php

namespace Vendor\FilterPreference\Model\Layer\Filter;

use Magento\Framework\Stdlib\Cookie\CookieMetadataFactory;
use Magento\Framework\Stdlib\CookieManagerInterface;

/**
 * Layer attribute filter
 */
class Attribute extends \Magento\CatalogSearch\Model\Layer\Filter\Attribute {

    const COOKIE_NAME = 'appliedfilter';
    const COOKIE_DURATION = 86400; // One day (86400 seconds)

    /**
     * @param \Magento\Catalog\Model\Layer\Filter\ItemFactory $filterItemFactory
     * @param \Magento\Store\Model\StoreManagerInterface $storeManager
     * @param \Magento\Catalog\Model\Layer $layer
     * @param \Magento\Catalog\Model\Layer\Filter\Item\DataBuilder $itemDataBuilder
     * @param \Magento\Framework\Filter\StripTags $tagFilter
     * @param \Magento\Framework\Stdlib\CookieManagerInterface $cookieManager
     * @param \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory $cookieMetadataFactory
     * @param array $data
     */

    public function __construct(
    \Magento\Catalog\Model\Layer\Filter\ItemFactory $filterItemFactory, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Catalog\Model\Layer $layer, \Magento\Catalog\Model\Layer\Filter\Item\DataBuilder $itemDataBuilder, \Magento\Framework\Filter\StripTags $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 \Magento\Framework\App\RequestInterface $request
     * @return $this
     * @throws \Magento\Framework\Exception\LocalizedException
     */
    public function apply(\Magento\Framework\App\RequestInterface $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 \Magento\CatalogSearch\Model\ResourceModel\Fulltext\Collection $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;
    }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.