Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento 2 : I want to add ifconfig in override block xml

I tried to override block with phtml file from

vendor\magento\module-checkout\view\frontend\success.phtml

to my file

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

and xml file is 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>

When i have enable module from configuration then execute with my block and phtml file.

when disable module then execute the default file and block.

But default file and block not execute when i have disable module from configuration.

Please help me and solve this issue

Thanks in advance.

like image 330
Sandip Dutt Avatar asked Nov 11 '17 11:11

Sandip Dutt


People also ask

How do I override config xml?

To override configuration settings, create a ConfigOverride. xml file and place it in the www\BIWebsite\App_Data folder of your Dundas BI instance. In a default installation of Dundas BI, there is a sample file you can start off with named ConfigOverride. xml.

How do I override information Phtml file in Magento 2?

If you need to override the info template with your module or theme level, you can do it with ease. Template declaration will be found from the Block class, Magento\Sales\Block\Order\Info with protected $_template variable. Add the content from the Original template with your modified changes.


1 Answers

if config will show template only when it has value true, it does not work as else condition.

In order to solve your problem, I suggest you creating a helper function and add the conditions in the helper method.

Something like this:

<?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;
    }
} 

then include your block in the layout Instead of this

<referenceBlock name="checkout.success">
    <action method="setTemplate">
        <argument name="template" xsi:type="helper" helper="Custom\Module\Helper\Data::getTemplate"></argument>
    </action>
</referenceBlock>
like image 83
Piyush Avatar answered Oct 19 '22 12:10

Piyush