0

In magento 2.4 we can not add out of stock product while placing order from admin. I have requirement that admin should be able to order from admin side even the products are out of stock. I see the backorder but I want it only for admin side.

3
  • This is the M2 core bug.
    – Sohel Rana
    Nov 24, 2020 at 12:13
  • Do you have any solution? Nov 24, 2020 at 12:14
  • 1
    Posted, try now
    – Sohel Rana
    Nov 24, 2020 at 12:17

3 Answers 3

0

Try the following way until M2 fixes that core bug.

app/code/SR/Sales/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\InventorySales\Model\IsProductSalableForRequestedQtyCondition\IsSalableWithReservationsCondition">
        <plugin name="sr_disable_stock_check_for_admin"
                type="SR\Sales\Plugin\Model\InventorySales\IsProductSalableForRequestedQtyCondition\IsSalableWithReservationsCondition"
                sortOrder="1"/>
    </type>
</config>

app/code/SR/Sales/Plugin/Model/InventorySales/IsProductSalableForRequestedQtyCondition/IsSalableWithReservationsCondition.php

<?php

declare(strict_types=1);

namespace SR\Sales\Plugin\Model\InventorySales\IsProductSalableForRequestedQtyCondition;

use Magento\InventorySales\Model\IsProductSalableForRequestedQtyCondition\IsSalableWithReservationsCondition as BaseIsSalableWithReservationsCondition;
use Magento\InventorySalesApi\Api\Data\ProductSalableResultInterface;
use Magento\InventorySalesApi\Api\Data\ProductSalableResultInterfaceFactory;

class IsSalableWithReservationsCondition
{
    /**
     * @var ProductSalableResultInterfaceFactory
     */
    private $productSalableResultFactory;

    /**
     * IsSalableWithReservationsCondition constructor.
     *
     * @param ProductSalableResultInterfaceFactory $productSalableResultFactory
     */
    public function __construct(
        ProductSalableResultInterfaceFactory $productSalableResultFactory
    ) {
        $this->productSalableResultFactory = $productSalableResultFactory;
    }

    /**
     * @param BaseIsSalableWithReservationsCondition $subject
     * @param \Closure $proceed
     * @param string $sku
     * @param int $stockId
     * @param float $requestedQty
     * @return ProductSalableResultInterface
     */
    public function aroundExecute(
        BaseIsSalableWithReservationsCondition $subject,
        \Closure $proceed,
        string $sku,
        int $stockId,
        float $requestedQty
    ): ProductSalableResultInterface {
        return $this->productSalableResultFactory->create(['errors' => []]);
    }
}
7
  • Not working. While adding product it is not coming in this function. Nov 24, 2020 at 12:54
  • This is the working code. Maybe you made some mistake. You need to adjust this code into a new custom module.
    – Sohel Rana
    Nov 24, 2020 at 13:46
  • I added log it should generate log file, but it is not generating. Are you sure this function is running while adding product while placing order from admin ? Nov 25, 2020 at 13:08
  • I tried, I created new module with same code but it is not working. My magento version is 2.4.1 Nov 26, 2020 at 11:40
  • I will check with M2.4.1 CE later.
    – Sohel Rana
    Nov 26, 2020 at 14:55
0

enter image description here

just comment this code, to make out of stock work in admin

1
  • Lacks context no longer relevant for M2.4 equivalent classes
    – Flipmedia
    Aug 21, 2023 at 18:24
0

My solution: change function get Config backorders

1- Create di.xml file in your module Vendor/Module/etc/di.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">

<type name="Magento\CatalogInventory\Model\Configuration">
    <plugin name="custom_get_backorders" type="Vendor\Module\Plugin\CustomBackorders" sortOrder="1" disabled="false"/>
</type>

2- Create CustomBackorders.php file

Vendor/Module/Plugin/CustomBackorders.php

<?php

namespace Vendor\Module\Plugin;

use Magento\Backend\Model\Auth\Session as BackendSession;

class CustomBackorders
{

    /**
     * @var BackendSession
     */
    protected $_backendSession;

    public function __construct(
        BackendSession $backendSession
    )
    {
        $this->_backendSession = $backendSession;
    }

    public function afterGetBackorders(\Magento\CatalogInventory\Model\Configuration $subject, $result)
    {
        if ($this->_backendSession->isLoggedIn()) {
            $result = (int)1;
        }

        return $result;
    }
}
2
  • Hi - please may I ask which Magento 2 version does this work for? I have tried it on Magento 2.4.3 and it isn't working for me.
    – John
    Jun 15, 2022 at 7:27
  • Does not fix/allow admin to add out of stock product for M2.4.5-p4 with MSI modules disabled
    – Flipmedia
    Aug 21, 2023 at 18:23

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.