Question

I am trying to override abstract class controller magento\module-sales\Controller\AbstractController\Reorder.php with my controller. Below are the code

New custom module called ReorderButton

module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Vistar_ReorderButton"/>
</config>

di.xml

<?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\Controller\AbstractController\Reorder" type = "Vistar\ReorderButton\Controller\Order\Reorder"/>
   
</config>

reorder.php (RerorderButton/Controller/Order/Reorder.php)

<?php

namespace Vistar\ReorderButton\Controller\Order;

use Magento\Framework\App\Action;
use Magento\Framework\Registry;
use Magento\Framework\App\Action\HttpPostActionInterface;

class Reorder extends \Magento\Sales\Controller\AbstractController\Reorder
{
    /**
     * @var \Magento\Sales\Controller\AbstractController\OrderLoaderInterface
     */
    protected $orderLoader;

    /**
     * @var Registry
     */
    protected $_coreRegistry;

    protected $_stockItemRepository;

    /**
     * @param Action\Context $context
     * @param OrderLoaderInterface $orderLoader
     * @param Registry $registry
     */
    public function __construct(
        Action\Context $context,
        OrderLoaderInterface $orderLoader,
        Registry $registry,
        \Magento\CatalogInventory\Model\Stock\StockItemRepository $stockItemRepository
    ) {
        $this->orderLoader = $orderLoader;
        $this->_coreRegistry = $registry;
        $this->_stockItemRepository = $stockItemRepository;
        parent::__construct($context, $orderLoader, $registry);
    }

    /**
     * Action for reorder
     *
     * @return \Magento\Framework\Controller\ResultInterface
     */
    public function execute()
    {
        $result = $this->orderLoader->load($this->_request);
        if ($result instanceof \Magento\Framework\Controller\ResultInterface) {
            return $result;
        }
        $order = $this->_coreRegistry->registry('current_order');
        /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
        $resultRedirect = $this->resultRedirectFactory->create();

        /* @var $cart \Magento\Checkout\Model\Cart */
        $cart = $this->_objectManager->get(\Magento\Checkout\Model\Cart::class);
        $items = $order->getItemsCollection();
        foreach ($items as $item) {
            $_productStock = $this->_stockItemRepository->get($item->getProductId());
            if($_productStock->getIsInStock()) {
                try {
                    
                    $cart->addOrderItem($item);
        
                } catch (\Magento\Framework\Exception\LocalizedException $e) {
                    if ($this->_objectManager->get(\Magento\Checkout\Model\Session::class)->getUseNotice(true)) {
                        $this->messageManager->addNoticeMessage($e->getMessage());
                    } else {
                        $this->messageManager->addErrorMessage($e->getMessage());
                    }
                    return $resultRedirect->setPath('*/*/history');
                } catch (\Exception $e) {
                    $this->messageManager->addExceptionMessage(
                        $e,
                        __('We can\'t add this item to your shopping cart right now.')
                    );
                    return $resultRedirect->setPath('checkout/cart');
                }
            }
        }

        $cart->save();
        return $resultRedirect->setPath('checkout/cart');
    }
}

I tried running php bin/magento setup:di:compile, I got this error

 Class Learning\ReorderButton\Controller\Order\OrderLoaderInterface does not exist
  Class Learning\ReorderButton\Controller\Order\Reorder\Interceptor generation error: The requested class did not gener
  ate properly, because the 'generated' directory permission is read-only. If --- after running the 'bin/magento setu
  p:di:compile' CLI command when the 'generated' directory permission is set to write --- the requested class did not
   generate properly, then you must add the generated class object to the signature of the related construct method,
  only.

Is there anything I am missing?

Was it helpful?

Solution

You can use Plugin to override the abstract class controller. But I ended up overriding /* @var $cart \Magento\Checkout\Model\Cart */ by using AroundPlugin.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top