How to add Enable Disable functionality for a referenceBlock in Magento 2
-
14-04-2021 - |
Question
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.
referenceBlock
is located at app/design/frontend/ProjectName/store/Magento_ProductRecommendationsLayout/layout/catalog_product_view.xml
No correct solution
OTHER TIPS
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" />
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>