【问题标题】:Magento 2 : I want to add ifconfig in override block xmlMagento 2:我想在覆盖块 xml 中添加 ifconfig
【发布时间】:2018-04-24 12:47:01
【问题描述】:

我尝试使用来自

的 phtml 文件覆盖块

供应商\magento\module-checkout\view\frontend\success.phtml

到我的文件

app\code\Custom\Module\view\frontend\checkout\success.phtml

xml 文件是 checkout_onepage_success.xml

<referenceBlock name="checkout.success">
       <action method="setTemplate" ifconfig="custom_general/general/active">
            <argument name="template" xsi:type="string">Custom_Module::checkout/success.phtml</argument>
        </action>
</referenceBlock>

当我从配置中启用模块时,然后使用我的块和 phtml 文件执行。

当禁用模块时,执行默认文件并阻塞。

但是当我从配置中禁用模块时,默认文件和块不会执行。

请帮助我解决这个问题

提前致谢。

【问题讨论】:

    标签: php xml magento magento2 magento2.1


    【解决方案1】:

    如果 config 仅在其值为 true 时显示模板,则它不能作为 else 条件。

    为了解决你的问题,我建议你创建一个辅助函数,并在辅助方法中添加条件。

    类似这样的:

    <?php
    
    namespace Custom\Module\Helper;
    
    class Data extends \Magento\Framework\App\Helper\AbstractHelper
    {
        protected $_request;
    
        public function __construct
        (
            \Magento\Framework\App\Request\Http $request
        ) {
            $this->_request = $request;
        }
    
        public function getTemplate()
        {
            if ($this->_scopeConfig->isSetFlag('custom_general/general/active')) {
                $template =  'Custom_Module::checkout/success.phtml';
            } else {
                $template = 'Vendor_Module::checkout/success.phtml';
            }
    
            return $template;
        }
    } 
    

    然后在布局中包含您的块 而不是这个

    <referenceBlock name="checkout.success">
        <action method="setTemplate">
            <argument name="template" xsi:type="helper" helper="Custom\Module\Helper\Data::getTemplate"></argument>
        </action>
    </referenceBlock>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-18
      • 1970-01-01
      • 2014-12-31
      • 2023-03-22
      • 2012-11-27
      • 1970-01-01
      • 1970-01-01
      • 2013-10-15
      相关资源
      最近更新 更多