TransWikia.com

How to add Enable Disable functionality for a referenceBlock in Magento 2

Magento Asked by Kumar Gaurav on December 9, 2020

I want to add Enable/disable functionality for a referenceBlock. <referenceBlock name="product_recommendations_pdp" remove="false" />
The referenceBlock can be remove="true" or remove="false" directly form admin-panel for just selecting.enter image description here

referenceBlock is located at app/design/frontend/ProjectName/store/Magento_ProductRecommendationsLayout/layout/catalog_product_view.xml

2 Answers

You can try a different approach to solve your issue.
I assume that you are using <referenceBlock name="product_recommendations_pdp" to add a block or execute an action on the specified block.
You can add the ifconfig attribute on your block or action tags.

<referenceBlock name="product_recommendations_pdp">
    <action method="setTemplate" ifconfig="section_name/group_name/field_name">
        <argument name="setTemplate" xsi:type="string"> your_template_path</argument>
    </action>
    <!-- or -->
    <block class="your_block_class_name" ifconfig="section_name/group_name/field_name">...</block>
</referenceBlock>

Answered by Rakesh Patel on December 9, 2020

Use ifconfig attribute

ifconfig will not work with <referenceBlock> tag. So you need to call the block again in your custom layout file and add ifconfig attribute with your config path. The blocks with same name gets eventually merged as one.

<block class="your_block_class" name="your_block_name" ifconfig="section_name/group_name/field_name" template="your_template_path" />

Answered by Lokesh Naik on December 9, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP