0

Using observer how to set catalog categories content Add CMS Block (category.description) programmatically

My observer:

<?php
namespace Zero\DescBlock\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();
        }
    }
}

Condition : Here my filterLabel is, apple and orange if filterLabel is apple i need to set cms block identifier my_apple if orange cms block identifier my_orange.

I am added cms block to Product listing page category description section,.

path :

app\design\frontend\Zero\mytheme\Magento_Catalog\layout\catalog_category_view.xml

<referenceContainer name="content">
    <block class="Magento\Cms\Block\Block" name="category.description">
        <arguments>
            <argument name="block_id" xsi:type="string">cms_block_identifier</argument>
        </arguments>
    </block>
</referenceContainer>

How to merge into my observer?

8
  • if customer can add 2 filter at a time then ??? Commented Jan 30, 2021 at 6:32
  • Take last attribute.
    – zus
    Commented Jan 30, 2021 at 6:35
  • 1 more quation you create all block for filter ??? Commented Jan 30, 2021 at 6:36
  • I created normal cms block, based on that attributes i need to set my cms block.
    – zus
    Commented Jan 30, 2021 at 6:37
  • like my_red,my_orange,my_white etc ?? Commented Jan 30, 2021 at 6:37

0

Your Answer

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