0

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.

7
  • please upload your whole block code and system.log Oct 16, 2018 at 7:14
  • @RohanHapani, updated my code, getting this error, Fatal error: Class 'Vendor\Module\Block\Order\Magento\Sales\Block\Order\History' not found on on line 11 Oct 16, 2018 at 7:15
  • did you run php bin/magento setup:di:compile ??
    – MR.R DNATH
    Oct 16, 2018 at 7:55
  • yes, throwing above error once complied, am i using correctly? Oct 16, 2018 at 7:57
  • 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 Oct 16, 2018 at 8:07

1 Answer 1

3

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/');
    }
}
3
  • Yes tried your code, same error again Oct 16, 2018 at 14:19
  • Check updated answer.
    – Sohel Rana
    Oct 16, 2018 at 15:21
  • after this getting Invalid template file: 'order/history.phtml' in module: 'Vendor_Module' block's name: 'sales.order.history' Oct 27, 2018 at 11:28

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.