0

Reorder button doesn't show up when an item is out of stock. How can I make the code to ignore this condition and allow customer to reorder by excluding the out of stock item.

enter image description here

The "Reorder" button only appears on some orders in order history.

The method responsible for reordering is Mage_Sales_Model_Order::_canReorder(). The method _canReorder is called by Mage_Sales_Model_Order::canReorder(). This is actually called to see if you can reorder. Inside the canReorder() function:

if (!$ignoreSalable && !$product->isSalable()) {
    return false;
}

"isSalable" checks product’s “status”, stock availability.

Right now, how the "reorder" button works is that, if an order history X has n items. if 1 item out of the n items is out-of-stock, the reorder button doesn't appear. I'm wondering how can I make the reorder button to always appear, so when a customer clicks on the reorder button, it will create the new shopping cart with the items that are available.

Can I just remove the "isSalable()" from this if statement?

1 Answer 1

0

Yes, you need to remove this condition(!$product->isSalable()) from there and also check the controller action where add to cart the previous order item. You need to by-pass the available product to add to cart with multiple product.

By-pass means you need to overwrite this function as below

Actual file: vendor/magento/module-sales/Controller/AbstractController\Reorder.php

Overwrite process to actual file

  1. create a file as app/code/Vendor/Module/etc/di.xml. And put the below code
<preference for = "Magento\Sales\Controller\AbstractController\Reorder" type = "Vendor\Module\Controller\Order\Reorder" />
  1. Create file as app/code/Controller/Order/Reorder.php and put the below code
<?php

namespace Vendor\Module\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');
    }
}
4
  • sorry, but what do you mean by "You need to by-pass the available product to add to cart with multiple product."? Sep 1, 2020 at 16:31
  • I have updated the post. Please try Sep 2, 2020 at 15:01
  • Sorry, it's been a pretty long time but I am continuing on this. For step 2, shouldn't the file be in the app/code/vendor/module/controller/order/reorder.php? Sep 21, 2020 at 18:38
  • and also, how can I override the order.php file to remove this condition(!$product->isSalable()) from there? Sep 21, 2020 at 18:52

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.