6

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.

Sandip Dutt
  • 175
  • 1
  • 3
  • 8

1 Answers1

9

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>
Piyush
  • 457
  • 7
  • 18