0

I am using event ( catalog_product_collection_load_before )for check PLP page layered navigation attributes selected or not, my observer,

app\code\Zero\DescBlock\Observer\Data.php

<?php

namespace Zero\DescBlock\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Catalog\Model\Layer\Resolver as LayerResolver;
use Psr\Log\LoggerInterface;

class Data implements ObserverInterface
{
    /**
     * @var \Magento\Catalog\Model\Layer\Category
     */
    protected $catalogLayer;
    
    /**
     * @var \Psr\Log\LoggerInterface
     */
    private $logger;

    /**
     * @param LayerResolver $layerResolver,
     */
    public function __construct(
        LayerResolver $layerResolver,
        LoggerInterface $logger
    ) {
        $this->catalogLayer = $layerResolver->get();
        $this->logger = $logger;
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        
        // Get selected filters
        $layer         = $this->catalogLayer;
        $activeFilters = $layer->getState()->getFilters();
        
        // checking variable activeFilters using log
        $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/custom.log');  
        $logger = new \Zend\Log\Logger();  
        $logger->addWriter($writer);  
        $logger->info('Your log details: ' .$activeFilters); 

    }
}

Log not return anything.

Any help thanks?

1
  • Hello sorry for the late reply please check answer and let me know if any query :) – Rk Rathod Jan 29 at 11:43
1

Try this :-

app/code/Vendor/Module/etc/frontend/events.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <!-- event catalog_product_collection_load_after -->
    <event name="catalog_product_collection_load_after">
        <observer name="rk_rathod_product_available"
                  instance="Vendor\Module\Observer\CollectionObserver"/>
    </event>
</config>

app/code/Vendor/Module/Observer/CollectionObserver.php

<?php
namespace Vendor\Module\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;

/**
 * Class CollectionObserver
 * @package Vendor\Module\Observer
 */
class CollectionObserver implements ObserverInterface
{
    /**
     * @var \Magento\Catalog\Model\Layer\Resolver
     */
    protected $layerResolver;

    /**
     * CollectionObserver constructor.
     * @param \Magento\Catalog\Model\Layer\Resolver $layerResolver
     */
    public function __construct(

        \Magento\Catalog\Model\Layer\Resolver $layerResolver
    ) {
        $this->layerResolver = $layerResolver;
    }

    /**
     * @param Observer $observer
     */
    public function execute(Observer $observer)
    {
        $layer = $this->layerResolver->get();
        $activeFilters = $layer->getState()->getFilters();
        foreach ($activeFilters as $filter)
        {
           $filterLabel = $filter->getLabel();
           $filterValue = $filter->getValue();
           $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/custom.log');  
           $logger = new \Zend\Log\Logger();  
           $logger->addWriter($writer);  
           $logger->info('Filter Label: ' .$filterLabel); 
           $logger->info('Filter Value: ' .$filterValue);
        }
    }
}
14
  • Still custom.log not generated. – zus Jan 29 at 11:51
  • please check updated answer – Rk Rathod Jan 29 at 11:52
  • Thank you for your time and effort, really thank you. – zus Jan 29 at 12:01
  • 1
    Most Welcome :) – Rk Rathod Jan 29 at 12:02
  • Actually i am doing this task for my another task, like if one attribute selected the relevant cms block set to the category description. can you pls help me this also magento.stackexchange.com/q/329971/57334 – zus Jan 29 at 12:04

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.