4

I am trying to override /module-sales/view/frontend/templates/order/view.phtml by di.xml

<preference for="Magento\Sales\Block\Order\View" type="Myvendor\Mymodule\Block\Order\View"/>

The Block i added is

namespace Myvendor\Mymodule\Block\Order;

use Magento\Customer\Model\Context;


class View extends \Magento\Sales\Block\Order\View
{
    /**
     * Core registry
     *
     * @var \Magento\Framework\Registry
     */
    protected $_coreRegistry = null;

    /**
     * @var \Magento\Customer\Model\Session
     */
    protected $_customerSession;

    /**
     * @var \Magento\Payment\Helper\Data
     */
    protected $_paymentHelper;

    /**
     * @param \Magento\Framework\View\Element\Template\Context $context
     * @param \Magento\Framework\Registry $registry
     * @param \Magento\Framework\App\Http\Context $httpContext
     * @param \Magento\Payment\Helper\Data $paymentHelper
     * @param array $data
     */

    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Framework\Registry $registry,
        \Magento\Framework\App\Http\Context $httpContext,
        \Magento\Payment\Helper\Data $paymentHelper,
    \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
        array $data = []
    ) {
        $this->_paymentHelper = $paymentHelper;
        $this->_coreRegistry = $registry;
        $this->httpContext = $httpContext;
        $this->scopeConfig = $scopeConfig;
    parent::__construct($context,$registry,$httpContext, $paymentHelper, $data);
        $this->_isScopePrivate = true;
    }




    public function getTemplate() 
    {
            $storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORE;
            $customOrderViewConfig = $this->scopeConfig->getValue('customorderview/enabled/fieldid', $storeScope);
            if ($customOrderViewConfig) {
                return 'corder/view.phtml';
            }
            return parent::getTemplate();
    }

    public function setTemplate($template)
    {
            $storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORE;
            $customOrderViewConfig = $this->scopeConfig->getValue('customorderview/enabled/fieldid', $storeScope);
            if ($customOrderViewConfig == 1) {
            $_template = 'corder/view.phtml';
            }
        else
        {
                $template = 'Magento_Sales::order/view.phtml';
        }
        return parent::setTemplate($_template);
    }
}

This is working fine , but i want to load my Phtml file when my module is enabled.

I tried

  • If condition in phtml file looks bad.
  • No way to add ifConfig in layout ( There is no layout in my module ).
  • beforeToHtml function in block ( Not working ).
  • setTemplate function in block ( Not working ).

Any Help would be appreciated.

1 Answer 1

2

You can try one of this variants:

Override getTemplate method:

public function getTemplate() 
{
    $isEnabled = $this->_scopeConfig->getValue(
            'module/path/enabled',
            \Magento\Store\Model\ScopeInterface::SCOPE_STORE
        );
    if ($isEnabled) {
        //Return your template
        return 'path/to/custom/template.phtml';
    }

    return parent::getTemplate();
}

Override toHtml method:

public function toHtml()
{
    $isEnabled = $this->_scopeConfig->getValue(
            'module/path/enabled',
            \Magento\Store\Model\ScopeInterface::SCOPE_STORE
        );
    if (!$isEnabled) {
        //Return your template
        return parent::toHtml();
    }

    //Some actions
}

And I am surprised, why not work setTemplate, but it working for me:

public function setTemplate($template)
{
    if ($isEnabled) {
        //Your custom template
        $template = 'path/to/template.phtml';
    }

    return parent::setTemplate($template);
}

This all working for me, and i hope, that one of these methods help you.

3
  • @ Irop Thanks for quick reply, when i tried getTemplate , am getting two templates into front end. setTemplate and toHtml functions are still not working. I updated my Block i used above function individually but i mentioned for reference. Commented Jun 4, 2016 at 18:27
  • When you set template in $_template it working for you? In these case you can try set template in __constructor : if ($moduleEnabled) { $this->_template = 'path/to/template.phtml'; }. Maybe it help for you Commented Jun 4, 2016 at 18:46
  • @ Irop I have tried inside __constructor also but no luck. Commented Jun 4, 2016 at 18:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.