Overriding Sales\Block\Order\View Block in Magento 2
-
29-09-2020 - |
Domanda
I am trying to extend Magento\Sales\Block\Order\View
by using preferences to get some more information into customer order view page.
Everything works fine if i remove __construct. so what might be the root cause of this issue?
Have been following lot of answers from community but no luck. ( removed cache,generation and done upgrade ) link
Errors :
From report : Object DOMDocument should be created.
From log :
Recoverable Error: Argument 2 passed to Magento\Sales\Block\Order\View::__construct() must be an instance of Magento\Framework\Registry,
namespace Myvendor\Mymodule\Block\Order;
use Magento\Customer\Model\Context;
/**
* Sales order view block
*/
class View extends \Magento\Sales\Block\Order\View
{
// Some protected properties
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,
array $data = []
) {
$this->_paymentHelper = $paymentHelper;
$this->_coreRegistry = $registry;
$this->httpContext = $httpContext;
parent::__construct($context,$paymentHelper,$registry,$httpContext, $data);
$this->_isScopePrivate = true;
}
Any help would be appreciated.
Soluzione
This is caused by the fact that the payment helper you inject via dependency injection is not at the right position of the original constructor.
To fix that you need to replace:
parent::__construct($context,$paymentHelper,$registry,$httpContext, $data);
With:
parent::__construct($context,$registry,$httpContext, $paymentHelper, $data);