Magento 2 – Override Sales Order History Issue

blocksmagento2order-historyorders

I am trying to override sales order history block into custom module.

Below code i used in di.xml file.

 <?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Sales\Block\Order\History"   type="Vendor\Module\Block\Order\History" />

 </config>

And the block file is like below.

namespace Vendor\Module\Block\Order;

use \Magento\Framework\App\ObjectManager;
use \Magento\Sales\Model\ResourceModel\Order\CollectionFactoryInterface;

 class History extends \Magento\Sales\Block\Order\History
 {
/**
 * @var string
 */
// protected $_template = 'order/history.phtml';

/**
 * @var \Magento\Sales\Model\ResourceModel\Order\CollectionFactory
 */
protected $_orderCollectionFactory;

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

/**
 * @var \Magento\Sales\Model\Order\Config
 */
protected $_orderConfig;

/**
 * @var \Magento\Sales\Model\ResourceModel\Order\Collection
 */
protected $orders;

/**
 * @var CollectionFactoryInterface
 */
private $orderCollectionFactory;

/**
 * @param \Magento\Framework\View\Element\Template\Context $context
 * @param \Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderCollectionFactory
 * @param \Magento\Customer\Model\Session $customerSession
 * @param \Magento\Sales\Model\Order\Config $orderConfig
 * @param array $data
 */
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,
    array $data = []
) {
    $this->_orderCollectionFactory = $orderCollectionFactory;
    $this->_customerSession = $customerSession;
    $this->_orderConfig = $orderConfig;
    parent::__construct($context, $data);
}

/**
 * @return void
 */
protected function _construct()
{
    parent::_construct();
    $this->pageConfig->getTitle()->set(__('My Orders'));
}

/**
 * @return CollectionFactoryInterface
 *
 * @deprecated 100.1.1
 */
private function getOrderCollectionFactory()
{
    if ($this->orderCollectionFactory === null) {
        $this->orderCollectionFactory = ObjectManager::getInstance()->get(CollectionFactoryInterface::class);
    }
    return $this->orderCollectionFactory;
}

/**
 * @return bool|\Magento\Sales\Model\ResourceModel\Order\Collection
 */
public function getOrders()
{
    if (!($customerId = $this->_customerSession->getCustomerId())) {
        return false;
    }
    if (!$this->orders) {
        $this->orders = $this->getOrderCollectionFactory()->create($customerId)->addFieldToSelect(
            '*'
        )->addFieldToFilter(
            'status',
            ['in' => $this->_orderConfig->getVisibleOnFrontStatuses()]
        )->setOrder(
            'created_at',
            'desc'
        );
    }
    return $this->orders;
}

/**
 * @return $this
 */
protected function _prepareLayout()
{
    parent::_prepareLayout();
    if ($this->getOrders()) {
        $pager = $this->getLayout()->createBlock(
            \Magento\Theme\Block\Html\Pager::class,
            'sales.order.history.pager'
        )->setCollection(
            $this->getOrders()
        );
        $this->setChild('pager', $pager);
        $this->getOrders()->load();
    }
    return $this;
}

/**
 * @return string
 */
public function getPagerHtml()
{
    return $this->getChildHtml('pager');
}

/**
 * @param object $order
 * @return string
 */
public function getViewUrl($order)
{
    return $this->getUrl('sales/order/view', ['order_id' => $order->getId()]);
}

/**
 * @param object $order
 * @return string
 */
public function getTrackUrl($order)
{
    return $this->getUrl('sales/order/track', ['order_id' => $order->getId()]);
}

/**
 * @param object $order
 * @return string
 */
public function getReorderUrl($order)
{
    return $this->getUrl('sales/order/reorder', ['order_id' => $order->getId()]);
}

/**
 * @return string
 */
 public function getBackUrl()
 {
    return $this->getUrl('customer/account/');
 }
}

Inside class added all the content of vendor block file.

getting below error once used above code.

  Vendor\Module\Block\Order\History
            Incompatible argument type: Required type: \Magento\Sales\Model\ResourceModel\Order\CollectionFactory. Actual type: array; File:

/app/code/Vendor/Module/Block/Order/History.php

Where i am wrong. its not overriding. Please anyone suggest me how to override this block file.

Best Answer

Change your __construct following way:


/**
 * @param \Magento\Framework\View\Element\Template\Context $context
 * @param \Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderCollectionFactory
 * @param \Magento\Customer\Model\Session $customerSession
 * @param \Magento\Sales\Model\Order\Config $orderConfig
 * @param array $data
 */
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,
    array $data = []
) {
    $this->_orderCollectionFactory = $orderCollectionFactory;
    $this->_customerSession = $customerSession;
    $this->_orderConfig = $orderConfig;
    parent::__construct($context, $orderCollectionFactory, $customerSession, $orderConfig, $data);
}

If you deployed then remove var/generation (M2.1) or generated/ (M2.2), clear cache.

[Update]

Replace following code:

Path: app/code/Vendor/Module/Block/Order/History.php


namespace Vendor\Module\Block\Order;

use \Magento\Framework\App\ObjectManager;
use \Magento\Sales\Model\ResourceModel\Order\CollectionFactoryInterface;

class History extends \Magento\Sales\Block\Order\History
{
    /**
     * @var string
     */
// protected $_template = 'order/history.phtml';

    /**
     * @var \Magento\Sales\Model\ResourceModel\Order\CollectionFactory
     */
    protected $_orderCollectionFactory;

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

    /**
     * @var \Magento\Sales\Model\Order\Config
     */
    protected $_orderConfig;

    /**
     * @var \Magento\Sales\Model\ResourceModel\Order\Collection
     */
    protected $orders;

    /**
     * @var CollectionFactoryInterface
     */
    private $orderCollectionFactory;

    /**
     * @param \Magento\Framework\View\Element\Template\Context $context
     * @param \Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderCollectionFactory
     * @param \Magento\Customer\Model\Session $customerSession
     * @param \Magento\Sales\Model\Order\Config $orderConfig
     * @param array $data
     */
    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,
        array $data = []
    ) {
        $this->_orderCollectionFactory = $orderCollectionFactory;
        $this->_customerSession = $customerSession;
        $this->_orderConfig = $orderConfig;
        parent::__construct($context, $orderCollectionFactory, $customerSession, $orderConfig, $data);
    }

    /**
     * @return void
     */
    protected function _construct()
    {
        parent::_construct();
        $this->pageConfig->getTitle()->set(__('My Orders'));
    }

    /**
     * @return CollectionFactoryInterface
     *
     * @deprecated 100.1.1
     */
    private function getOrderCollectionFactory()
    {
        if ($this->orderCollectionFactory === null) {
            $this->orderCollectionFactory = ObjectManager::getInstance()->get(CollectionFactoryInterface::class);
        }
        return $this->orderCollectionFactory;
    }

    /**
     * @return bool|\Magento\Sales\Model\ResourceModel\Order\Collection
     */
    public function getOrders()
    {
        if (!($customerId = $this->_customerSession->getCustomerId())) {
            return false;
        }
        if (!$this->orders) {
            $this->orders = $this->getOrderCollectionFactory()->create($customerId)->addFieldToSelect(
                '*'
            )->addFieldToFilter(
                'status',
                ['in' => $this->_orderConfig->getVisibleOnFrontStatuses()]
            )->setOrder(
                'created_at',
                'desc'
            );
        }
        return $this->orders;
    }

    /**
     * @return string
     */
    public function getPagerHtml()
    {
        return $this->getChildHtml('pager');
    }

    /**
     * @param object $order
     * @return string
     */
    public function getViewUrl($order)
    {
        return $this->getUrl('sales/order/view', ['order_id' => $order->getId()]);
    }

    /**
     * @param object $order
     * @return string
     */
    public function getTrackUrl($order)
    {
        return $this->getUrl('sales/order/track', ['order_id' => $order->getId()]);
    }

    /**
     * @param object $order
     * @return string
     */
    public function getReorderUrl($order)
    {
        return $this->getUrl('sales/order/reorder', ['order_id' => $order->getId()]);
    }

    /**
     * @return string
     */
    public function getBackUrl()
    {
        return $this->getUrl('customer/account/');
    }
}