If you are working in magento2, 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,
app/code/Webkul/Test/etc/frontend/events.xml
1 2 3 4 5 6 7 |
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd"> <event name="layout_generate_blocks_after"> <observer name="remove_block" instance="Webkul\Test\Observer\RemoveBlockForDiscount" /> </event> </config> |
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:
app/code/Webkul/Test/Observer/RemoveBlockForDiscount.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<?php 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('checkout.cart.coupon'); if ($block) { //you can apply or add you condition here. $layout->unsetElement('checkout.cart.coupon'); } } } |
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.
Hope this blog will help you in implementation of functionality.
Thank you.
Be the first to comment.