0

I want to get selected attributes in product listing page using observer,

app\code\Zero\DescBlock\registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(

    \Magento\Framework\Component\ComponentRegistrar::MODULE,

    'Zero_DescBlock', __DIR__ );

app\code\Zero\DescBlock\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 name="catalog_product_collection_load_before">
         <observer name="ChangeDescBlock" instance="Zero\DescBlock\Observer\Data" />
    </event>
</config>

app\code\Zero\DescBlock\etc\module.xml

<?xml version="1.0" ?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Zero_DescBlock" setup_version="1.0.0"/>
    <!-- <sequence>
        <module name="Magento_Catalog">
    </sequence> -->
</config>

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;

class Data implements ObserverInterface
{
    /**
     * @var \Magento\Catalog\Model\Layer\Category
     */
    protected $catalogLayer;

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

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        // Get selected filters
        $layer         = $this->catalogLayer;
        $activeFilters = $layer->getState()->getFilters();
        $this->logger->info(print_r( $activeFilters, true));

    }
}

The logger not return selected attributes.

1 Answer 1

0

Look at your php class. Logger is not defined.

<?php

namespace Zero\DescBlock\Observer;

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

class Data implements ObserverInterface
{
    /**
     * @var \Magento\Catalog\Model\Layer\Category
     */
    protected $catalogLayer;

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

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        // Get selected filters
        $layer         = $this->catalogLayer;
        $activeFilters = $layer->getState()->getFilters();
        $this->logger->info(print_r( $activeFilters, true));

    }
}

This SHOULD work but I have not tested.

<?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();
        $this->logger->info(print_r( $activeFilters, true));

    }
}

But why not use xdebug. print_r + log is alot of work to see the output.

23
  • I am selected one attribute in product listing page, may i know why its not available in syste.log file?
    – zus
    Jan 29, 2021 at 6:50
  • If we add " $this->logger->info(print_r( $activeFilters, true)); " the log should be paste in system.log file right?
    – zus
    Jan 29, 2021 at 6:50
  • in your terminal, run tail -f var/log/*.log and then run your request. Whichever log its printing in, it will show in the terminal. Jan 29, 2021 at 6:53
  • I am using windows, can't able to run your command.
    – zus
    Jan 29, 2021 at 6:54
  • It could be exception.log or debug.log. You can also run ls -altr var/log and the log that was most recently edited will show at the bottom of the list Jan 29, 2021 at 6:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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