Magento: Sử dụng điều kiện if/else trong magento 2 layout.

Magento: Sử dụng điều kiện if/else trong magento 2 layout.

Magento: Sử dụng điều kiện if/else trong magento 2 layout.

Bạn có thể sử dụng điều kiện IF/ELSE trong magento 2 layout. Điều kiện IF kiểu tra giá trị YES/NO trong magento 2 configuration.

Bạn cần tạo 1 module để tạo 1 configuration kiểu YES/NO. Và sử dụng trong layout xml.

<field id="enabled" type="select" translate="label,comment,tooltip" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
    <label>Enabled</label>
    <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
    <config_path>inviqa/general/enabled</config_path>
    <comment>Helpful message about using this field.</comment>
    <tooltip>Longer helpful message about using this field.</tooltip>
</field>

Sau đó thêm điều kiện vào if layout xml file:


<default>
    <block type="yourmodule/yourmodule" name="yourmodule.sidebar" before="-" template="yourmodule/block_sidebar.phtml"/>
    <reference name="left">        
        <action method="append" ifconfig="yoursection/yourgroup/yourfield_to_show_hide_left_sidebar">
            <block>yourmodule.sidebar</block>
        </action>
    </reference>        
    <reference name="right">
        <action method="append" ifconfig="yoursection/yourgroup/yourfield_to_show_hide_right_sidebar">    
            <block>yourmodule.sidebar</block>
        </action>    
    </reference>
</default>