Reading list Switch to dark mode

    Remove Block From Layout on Specific Condition in Magento 2

    Updated 15 February 2024

    If you are working in Magento 2, and want to remove block from layout without any condition, then you can read blog:

    Now, if you have a specific condition to display the block or not, then this can be achieve by observer.

    Firstly you need to create an events.xml file in your module,

    <?xml version="1.0"?>
    <config xmlns:xsi="" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
        <event name="layout_generate_blocks_after">
            <observer name="remove_block" instance="Webkul\Test\Observer\RemoveBlockForDiscount" />

    Here, layout_generate_blocks_after observer is called after a block is rendered on the page.
    And define the file to execute on observer.

    Now, create observer file:

    Searching for an experienced
    Magento 2 Company ?
    Find out More


    namespace Webkul\Test\Observer;
    use Magento\Framework\Event\Observer;
    use Magento\Framework\Event\ObserverInterface;
    class RemoveBlockForDiscount implements ObserverInterface
        public function execute(Observer $observer)
            /** @var \Magento\Framework\View\Layout $layout */
            $layout = $observer->getLayout();
            $block = $layout->getBlock('');
            if ($block) {
                //you can apply or add you condition here.

    Here, i am removing discount coupon block from the page.

    After adding code in your module, discount coupon block will get removed from pages, as from checkout/cart page.

    I hope this blog will help you with Remove Block From Layout on Specific Condition in Magento 2. You may also check our wide range of best Magento 2 Extensions.

    Please reach out to our team via a support ticket if you have any queries.

    Try this and if you have any queries then just comment below 🙂

    . . .

    Leave a Comment

    Your email address will not be published. Required fields are marked*

    Be the first to comment.

    Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home