3

I have created a custom module. which rewrites \Magento\Checkout\Block\Onepage\Success class by creating a custom module.

and i am able to make changes in prepareBlockData function but i want to inject helper class of my custom module in success block constructor below is the code i tried.

<?php

namespace Test\Buynow\Block\Magento\Checkout\Onepage;

use Magento\Customer\Model\Context;
use Magento\Sales\Model\Order;

class Success extends \Magento\Checkout\Block\Onepage\Success
{

    protected $_checkoutSession;


    protected $_orderConfig;


    protected $httpContext;


    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Checkout\Model\Session $checkoutSession,
        \Magento\Sales\Model\Order\Config $orderConfig,
        \Magento\Framework\App\Http\Context $httpContext,
        \Test\Buynow\Helper\Buynow $buynowHelper, //custom module helper class which i want to inject
        array $data = []
    ) {
        parent::__construct($context, $data);
        $this->_checkoutSession = $checkoutSession;
        $this->_orderConfig = $orderConfig;
        $this->_isScopePrivate = true;
        $this->httpContext = $httpContext;
        $this->buynow = $buynowHelper; //custom module helper class which i want to inject
    }


    protected function prepareBlockData()
    {
        $order = $this->_checkoutSession->getLastRealOrder();
        $isReference= false;

        if($order->getReference()):
            $isReference = true;
        endif;

        $this->addData(
            [
                'is_order_visible' => $this->isVisible($order),
                'view_order_url' => $this->getUrl(
                    'sales/order/view/',
                    ['order_id' => $order->getEntityId()]
                ),
                'print_url' => $this->getUrl(
                    'sales/order/print',
                    ['order_id' => $order->getEntityId()]
                ),
                'can_print_order' => $this->isVisible($order),
                'can_view_order'  => $this->canViewOrder($order),
                'order_id'  => $order->getIncrementId(),
                'reference'=> $isReference
            ]
        );
    }
}

after doing this i have flushed cache and deployed static content but i am getting below error.

PHP Fatal error: Uncaught TypeError: Argument 2 passed to Magento\Checkout\Block\Onepage\Success::__construct() must be an instance of Magento\Checkout\Model\Session,

3

Understanding correctly Dependency Injection in Magento2 can be tricky the first times. Take a look at this Magento2 extend core block constructor

So, in your case you are extending \Magento\Checkout\Block\Onepage\Success, which has following constructor (check it in your project better, maybe we use different versions & it may differ)

public function __construct(
    \Magento\Framework\View\Element\Template\Context $context,
    \Magento\Checkout\Model\Session $checkoutSession,
    \Magento\Sales\Model\Order\Config $orderConfig,
    \Magento\Framework\App\Http\Context $httpContext,
    array $data = []
) {
    ...

But, in your class constructor you are calling...

parent::__construct($context, $data);

Which causes error, as it should be...

parent::__construct($context, $checkoutSession, $orderConfig, $httpContext, $data);

So, your constructor should look like this...

public function __construct(
    \Magento\Framework\View\Element\Template\Context $context,
    \Magento\Checkout\Model\Session $checkoutSession,
    \Magento\Sales\Model\Order\Config $orderConfig,
    \Magento\Framework\App\Http\Context $httpContext,
    \Test\Buynow\Helper\Buynow $buynowHelper, //custom module helper class which i want to inject
    array $data = []
) {
    parent::__construct($context, $checkoutSession, $orderConfig, $httpContext, $data);
    $this->buynow = $buynowHelper; //custom module helper class which i want to inject
}

Note that you don't really need to redeclare parent protected variables ( $this->_checkoutSession, $this->_orderConfig, etc...), as they will be declared when you call parent constructor, and your class will be able to access them. This would be required only with private variables

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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