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 Answers
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' => []]);
}
}
-
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. 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
-
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;
}
}
-
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.– JohnJun 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 Aug 21 at 18:23