2

In version 2.2, the admin can create orders at backend for products that are out of stock or disabled. It tells the admin that this product you choose is out of stock but allows him to complete the order. How can i prevent this order from being made if the admin choose an out of stock product in the order or ordered a quantity for a product that doesnt have enough quantity. I hope someone can help me.

In Catalog>Inventory i set to hide products that are out of stock and set backorder to NO. but this only applied at frontend only not at the backend.

0

Try following way:

app/code/SR/MagentoStackExchange/etc/adminhtml/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">
    <type name="Magento\Sales\Block\Adminhtml\Order\Create\Search\Grid">
        <plugin name="SR_MagentoStackExchange::product_grid_create_order"
                type="SR\MagentoStackExchange\Plugin\Block\Sales\Adminhtml\Order\Create\Search\Grid" sortOrder="1"/>
    </type>
</config>

app/code/SR/MagentoStackExchange/Plugin/Block/Sales/Adminhtml/Order/Create/Search/Grid.php

<?php
    namespace SR\MagentoStackExchange\Plugin\Block\Sales\Adminhtml\Order\Create\Search;

    class Grid
    {
        /**
         * @var \Magento\CatalogInventory\Helper\Stock
         */
        private $stockFilter;

        /**
         * Grid constructor.
         *
         * @param \Magento\CatalogInventory\Helper\Stock $stockFilter
         */
        public function __construct(
            \Magento\CatalogInventory\Helper\Stock $stockFilter
        ) {
            $this->stockFilter = $stockFilter;
        }

        public function aroundSetCollection(
            \Magento\Sales\Block\Adminhtml\Order\Create\Search\Grid $subject,
            \Closure $proceed,
            $collection
        ) {
            $this->stockFilter->addInStockFilterToCollection($collection);
            $proceed($collection);
        }
    }
?>
  • This method didnt work fully (partial solution), Most of the products are configurable product. So for a simple product the out of stock product is hidden and the admin cant select it, this was done by your extension. But for configurable product the admin can still select the product by choosing the default configurable product and select configure and choose the product which is out of stock. After selecting the out of stock product a larg red X showed beneath the product Says that the product is out of stock. But the admin was able to make the order for this product. – mahdi danash Feb 12 at 13:40
  • Note: let say shoe size 43 is out of stock but 42 is in stock, in the grid the shoe 43 product is hidden from the grid but shoe 42 is shown also the default shoe that combines all sizes is shown, so the admin can choose the default shoe and press configure then chose size 43 which is out of stock then he can make the order( here is the problem). – mahdi danash Feb 12 at 13:50
  • I have an additional fix to add to the above fix you gave me, it is to override the file Quote.php found in magento/backend/mode/session by setting supermode to false instead of true $this->_quote->setIsSuperMode(false);. how can i override it? This will prevent an order being made if the item is out of stock only. But it will not prevent an order if it have for example 5 items in stock and i ordered 6 or more it will still allow such order. – mahdi danash Feb 12 at 17:56

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.