how to add negative qty product into cart in magento. 2
-
01-03-2021 - |
Question
How to add a out of stock (less than 0 ) product in shopping cart in magento 2
In my backorders setting i have custom option as Allow pre-order when i select a custom option and saved a backorder configuration after that how to add negative qty product into cart in magento.
Product edit page in admin
Store > configuration > catalog > inventory
I can't add a product into cart
Please some one help me to solve this
Solution
Try the following way:
app/code/SR/MagentoCommunity/etc/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\CatalogInventory\Model\StockStateProvider">
<plugin name="sr_custom_backorders"
type="SR\MagentoCommunity\Plugin\CatalogInventory\Model\StockStateProvider" sortOrder="1"/>
</type>
</config>
Suppose you custom option Allow Pre-Order value 5.
app/code/SR/MagentoCommunity/Plugin/CatalogInventory/Model/StockStateProvider.php
<?php
namespace SR\MagentoCommunity\Plugin\CatalogInventory\Model;
use Magento\CatalogInventory\Api\Data\StockItemInterface;
class StockStateProvider
{
/**
* @param \Magento\CatalogInventory\Model\StockStateProvider $subject
* @param $result
* @param StockItemInterface $stockItem
* @param $qty
* @return bool
*/
public function afterCheckQty(
\Magento\CatalogInventory\Model\StockStateProvider $subject,
$result,
StockItemInterface $stockItem,
$qty
) {
if (!$result) {
if ($stockItem->getQty() - $stockItem->getMinQty() - $qty < 0) {
switch ($stockItem->getBackorders()) {
case \Magento\CatalogInventory\Model\Stock::BACKORDERS_YES_NONOTIFY:
case \Magento\CatalogInventory\Model\Stock::BACKORDERS_YES_NOTIFY:
case 5:
return true;
default:
return false;
}
}
}
return $result;
}
}
[Update]
Using around plugin:
app/code/SR/MagentoCommunity/Plugin/CatalogInventory/Model/StockStateProvider.php
<?php
namespace SR\MagentoCommunity\Plugin\CatalogInventory\Model;
use Magento\CatalogInventory\Api\Data\StockItemInterface;
class StockStateProvider
{
/**
* @param \Magento\CatalogInventory\Model\StockStateProvider $subject
* @param \Closure $proceed
* @param StockItemInterface $stockItem
* @param $qty
* @return bool|mixed
*/
public function aroundCheckQty(
\Magento\CatalogInventory\Model\StockStateProvider $subject,
\Closure $proceed,
StockItemInterface $stockItem,
$qty
) {
$result = $proceed($stockItem, $qty);
if (!$result) {
if ($stockItem->getQty() - $stockItem->getMinQty() - $qty < 0) {
switch ($stockItem->getBackorders()) {
case \Magento\CatalogInventory\Model\Stock::BACKORDERS_YES_NONOTIFY:
case \Magento\CatalogInventory\Model\Stock::BACKORDERS_YES_NOTIFY:
case 5:
return true;
default:
return false;
}
}
}
return $result;
}
}
OTHER TIPS
1 .Set System - Configuration - Catalog - Inventory - Stock Options - Display Out of Stock Products to Yes.
2.Set System - Configuration - Catalog - Inventory - Product Stock Options - Backorder to Allow Qty Below 0.