cancel
Showing results for 
Search instead for 
Did you mean: 

Magento 2 - Using Observer how to Set CMS block to category description

Magento 2 - Using Observer how to Set CMS block to category description

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

 

Programmatically set CMS block for catalog categories add CMS block:

<?php
    echo $this->getLayout()
        ->createBlock(\Magento\Cms\Block\Block::class)
        ->setBlockId('my_cmsblock_identifier') //replace my_cmsblock_identifier with real CMS bock identifier
        ->toHtml();
?>

or

<referenceContainer name="content">
  <block class="Magento\Cms\Block\Block" name="unick_block_name">
    <arguments>
      <argument name="block_id" xsi:type="string">my_cmsblock_identifier</argument>
    </arguments>
  </block>
</referenceContainer>

If any better way to set pls suggest me regard set cms block to category description.

How to include into my observer.

 

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.

Any help thanks.