{"id":681,"date":"2019-11-20T07:34:27","date_gmt":"2019-11-20T07:34:27","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/2019\/11\/20\/remove-block-depending-on-config-setting-in-magento-2\/"},"modified":"2025-05-22T15:21:53","modified_gmt":"2025-05-22T09:51:53","slug":"remove-block-depending-on-config-setting-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/remove-block-depending-on-config-setting-in-magento-2\/","title":{"rendered":"How To Remove Block Depending on Config Setting in Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">I&#8217;ve earlier posted a solution to <a href=\"https:\/\/meetanshi.com\/blog\/remove-block-from-layout-in-magento-2\/\" target=\"_blank\" rel=\"noreferrer noopener\">remove a block from the layout in Magento 2<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">But what if you want to <strong>remove block depending on config setting in Magento 2<\/strong> store?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It may happen that based on certain conditions, you want to display a block or eliminate it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example, you want to disable the account registration based on a specific condition and hence remove the login block.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Implement the below method for similar scenarios and handle conditional behaviors like a pro!&nbsp; <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method to Remove Block Depending on Config Setting in Magento 2:<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">1. Create <strong><em>events.xml<\/em><\/strong> file in <strong><strong>app\\code\\[Vendor]\\[Module]\\etc<\/strong><\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?xml version=\"1.0\"?>\n&lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"urn:magento:framework:Event\/etc\/events.xsd\">\n    &lt;event name=\"layout_generate_blocks_after\">\n        &lt;observer name=\"remove_block\" instance=\"[Vendor]\\[Module]\\Model\\Observer\\RemoveBlock\" \/>\n    &lt;\/event>\n&lt;\/config><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">2. Create <em><strong>RemoveBlock.php<\/strong><\/em> file in <strong><strong>app\\code\\[Vendor]\\[Module]\\Model\\Observer<\/strong><\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n\nnamespace [Vendor]\\[Module]\\Observer;\n\nuse Magento\\Framework\\Event\\Observer;\nuse Magento\\Framework\\Event\\ObserverInterface;\nuse Magento\\Framework\\App\\Config\\ScopeConfigInterface;\nuse Magento\\Store\\Model\\ScopeInterface;\n\nclass RemoveBlock implements ObserverInterface\n{\n    protected $_scopeConfig;\n\n    public function __construct\n    (\n        ScopeConfigInterface $scopeConfig\n    )\n    {\n        $this->_scopeConfig = $scopeConfig;\n    }\n\n    public function execute(Observer $observer)\n    {\n        \/** @var \\Magento\\Framework\\View\\Layout $layout *\/\n        $layout = $observer->getLayout();\n        $block = $layout->getBlock('customer.new');  \/\/ here block reference name to remove\n\n        if ($block) {\n            $remove = $this->_scopeConfig->getValue('path_system_config', ScopeInterface::SCOPE_STORE);\n            if ($remove) {\n                $layout->unsetElement('customer.new');\n            }\n        }\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">That\u2019s it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It was never this easy to eliminate block depending on config setting in Magento 2!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Feel free to share the solution via social media with the fellow developers  <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thanks.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve earlier posted a solution to remove a block from the layout in Magento 2 But what if you want to remove block depending on&#8230;<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-681","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/681","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=681"}],"version-history":[{"count":3,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/681\/revisions"}],"predecessor-version":[{"id":15180,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/681\/revisions\/15180"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=681"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=681"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=681"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}