2

enter image description hereHow to not display out of stock products when we place reorder admin panel(Back-end) Please help me out.

1 Answer 1

2

You need to override the below file to your custom module:

/vendor/magento/module-sales/Model/AdminOrder/Create.php

and change the function initFromOrderItem like below:

public function initFromOrderItem(\Magento\Sales\Model\Order\Item $orderItem, $qty = null)
{
    if (!$orderItem->getId()) {
        return $this;
    }

    $product = $this->_objectManager->create(
        \Magento\Catalog\Model\Product::class
    )->setStoreId(
        $this->getSession()->getStoreId()
    )->load(
        $orderItem->getProductId()
    );


    $stockItem = $this->stockRegistry->getStockItem($orderItem->getProductId());
    if($stockItem->getData('is_in_stock') != 1){
        return $this;
    }

    if ($product->getId()) {
        $product->setSkipCheckRequiredOption(true);
        $buyRequest = $orderItem->getBuyRequest();
        if (is_numeric($qty)) {
            $buyRequest->setQty($qty);
        }
        $item = $this->getQuote()->addProduct($product, $buyRequest);
        if (is_string($item)) {
            return $item;
        }

        if ($additionalOptions = $orderItem->getProductOptionByCode('additional_options')) {
            $item->addOption(
                new \Magento\Framework\DataObject(
                    [
                        'product' => $item->getProduct(),
                        'code' => 'additional_options',
                        'value' => $this->serializer->serialize($additionalOptions)
                    ]
                )
            );
        }

        $this->_eventManager->dispatch(
            'sales_convert_order_item_to_quote_item',
            ['order_item' => $orderItem, 'quote_item' => $item]
        );
        return $item;
    }

    return $this;
}

No need to change anything.

9
  • Its not working for me Sukumar Gorai... Commented Sep 28, 2018 at 12:41
  • When i search for product that is out of stock should not display.. Sukumar Gorai... Commented Sep 28, 2018 at 12:48
  • This will not add out of products from that order. According to requirement this is correctly working. Try to change it in vendor folder and then test. Commented Sep 28, 2018 at 12:48
  • I have followed as above you said but when i place reorder from admin panel product of out stock its successfully order placed Sukumar Gorai.. Commented Sep 28, 2018 at 12:56
  • According to the answer it removes the product which is out of stock and added the products which are in stock. Clear the cache and test again. This will work from admin only. Commented Sep 28, 2018 at 13:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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