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;
}
}
cms static block or any other method.
other method?