0

I need to Hide static cms block "block-id-1" from frontpage programmatically customer group based. I need to add event to hide cms static block or any other method. I want to use admin interface to enable/disable customer group to hide "block-id-1".

Content of block-cms-id-1 is button which is currently display for all customer group on checkout/cart

Mainly I need to connect this block id to customer group which can be enable from admin>configuration>extension_name>Hide from groups

3
  • what is means by cms static block or any other method. other method?
    – Amit Bera
    Commented Jun 25, 2020 at 7:29
  • Backend admin>content>block> "block-cms-id-1" , Here block ID can be any name. I need to hide static block programmatically.
    – Ranjana
    Commented Jun 25, 2020 at 7:37
  • Here other method means if not possible to hide block ID, then I can call block ID to phtml file vendor/module/view/frontend/customfile.phtml Then even to hide html data group based. Need code to run vendor/module/observer/customfile to run event e.g. public function execute(Observer $observer) { if command ...} I've tried $layout = $observer->getLayout(); $block = $layout->getBlock('static-block-id-1'); This will not run as I think this code is for dynamic magento core ID. I've tried javascript to hide but unable to connect to group so that this can enable/disable from backend.
    – Ranjana
    Commented Jun 25, 2020 at 7:54

1 Answer 1

1

Magento renders a cms block using \Magento\Cms\Block\Block at frontend.

So, if you want to stop rendering a cms block based on customer group then create an after plugin on Magento\Cms\Block\Block:toHtml() and stop rendering the content for some customer groups.

Create di.xml at your module:

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Cms\Block\Block">
        <plugin disabled="false" name="Devamitbera_StackExchange_Plugin_Magento_Cms_Block_Block" sortOrder="10" type="Devamitbera\StackExchange\Plugin\Magento\Cms\Block\Block"/>
    </type>
</config>

##Plugin class

<?php

namespace Devamitbera\StackExchange\Plugin\Magento\Cms\Block;

use Magento\Customer\Model\Context as CustomerContext;

class Block
{
    /**
     * @var \Magento\Framework\App\Http\Context
     */
    protected $httpContext;

    public function __construct(
        \Magento\Framework\App\Http\Context $httpContext
    ){
        $this->httpContext = $httpContext;
    }
    public function afterToHtml(\Magento\Cms\Block\Block $subject, $result) {
        $blockId = $subject->getBlockId();
        $customerGroupId = $this->httpContext->getValue(CustomerContext::CONTEXT_GROUP);
        /**
         * If customer group id X match then
         */
        if($customerGroupId === '2'){
            return '';
        }
        return $result;
    }

    /***
     * Add Customer Group to cache
     * @param \Magento\Cms\Block\Block $subject
     * @param $result
     * @return mixed
     */
    public function afterGetCacheKeyInfo(\Magento\Cms\Block\Block $subject, $result)
    {
       if(!empty($result)){
           $result[] =  $this->httpContext->getValue(CustomerContext::CONTEXT_GROUP);
       }
        return $result;
    }
}
2
  • I've re-edited question in details. Please tell me in more details to implement, guidance would be greatly appreciated.
    – Ranjana
    Commented Jun 25, 2020 at 17:15
  • Any magento developer here to solve this complex implementation.
    – Ranjana
    Commented Jun 27, 2020 at 12:32

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.