Skip to main content
added 4 characters in body
Source Link
Amit Bera
  • 77.7k
  • 20
  • 124
  • 239

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

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

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

added 2218 characters in body
Source Link
Krishna ijjada
  • 9k
  • 6
  • 42
  • 70
namespace Myvendor\Mymodule\Block\Order;

use Magento\Customer\Model\Context; 


class View extends \Magento\Sales\Block\Order\View
{
    /**
     * SalesCore orderregistry
 view block   *
     * @var \Magento\Framework\Registry
     */
class View extends \Magento\Sales\Block\Order\View protected $_coreRegistry = null;
{
    /**
     * @var string\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);
    }
}
namespace Myvendor\Mymodule\Block\Order;

use Magento\Customer\Model\Context;

/**
 * Sales order view block
 */
class View extends \Magento\Sales\Block\Order\View
{
    /**
     * @var string
     */
    protected $_template = 'corder/view.phtml';
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);
    }
}
Source Link
Krishna ijjada
  • 9k
  • 6
  • 42
  • 70

Best way to load my module phtml only if system config is enabled

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;

/**
 * Sales order view block
 */
class View extends \Magento\Sales\Block\Order\View
{
    /**
     * @var string
     */
    protected $_template = 'corder/view.phtml';

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.