1

I want to override __prepareLayout function from 'Magento\Sales\Block\Order\History' so I used the preference.

In my di.xml

<preference for="Magento\Sales\Block\Order\History" type="Custom\Module\Block\Order\History"/>

In my History.php

class History extends \Magento\Sales\Block\Order\History
{
    protected function _prepareLayout()
    {
        parent::_prepareLayout();
        $this->setTemplate('Custom_Module::order/history.phtml');
        return $this;
    }

}

To this point my code is working fine now I want to inject custom Helper in this class.

So I added this-

    public function __construct(
        \Custom\Module\Helper\Data $helper
    ) {
        $this->helper   =   $helper;
    }

After Injecting Class the code is not working. I have the option to use ObjectManager, but it is not recommended, so I preferred to ask from Community if you have any better Option!

1
  • What is the error showing if you added helper in contruct Jul 1, 2020 at 5:51

1 Answer 1

3

you need to call parent constructor also with required parameters

public function __construct(
    \Magento\Framework\View\Element\Template\Context $context,
    \Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderCollectionFactory,
    \Magento\Customer\Model\Session $customerSession,
    \Magento\Sales\Model\Order\Config $orderConfig,
    \Custom\Module\Helper\Data $helper
) {
    $this->_orderCollectionFactory = $orderCollectionFactory;
    $this->_customerSession = $customerSession;
    $this->_orderConfig = $orderConfig;
    $this->helper   =   $helper;
    parent::__construct($context, $orderCollectionFactory, $customerSession, $orderConfig);
}
2
  • Correct solution. But should this be overridden since its an API class which means it should not be updated. Jul 1, 2020 at 6:38
  • 1
    yes this is correct solution +1
    – User 69
    Jul 1, 2020 at 6:52

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.