1

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

enter image description here

Store > configuration > catalog > inventory

enter image description here

I can't add a product into cart

enter image description here

Please some one help me to solve this

1

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;
    }
}
  • Fatal error: Uncaught TypeError: Argument 3 passed to Bootsgrid\Preorder\Plugin\CatalogInventory\Model\StockStateProvider::afterCheckQty() must implement interface Magento\CatalogInventory\Api\Data\StockItemInterface, none given, called in D:\wamp64\www\m2\magento\vendor\magento\framework\Interception\Interceptor.php on line 152 and defined in D:\wamp64\www\m2\magento\app\code\Cm\Preorder\Plugin\CatalogInventory\Model\StockStateProvider.php on line 15 – divya sekar May 17 at 5:14
  • It shows above error – divya sekar May 17 at 5:15
  • What is your magento version? – Sohel Rana May 17 at 5:22
  • Magento version is 2.1.3 – divya sekar May 17 at 5:27
  • Try using around plugin and remove generated dir. – Sohel Rana May 17 at 5:31
1
Stroes -> Configuration -> Catalog -> Inventory 

Click on Product stock Options Tab.

enter image description here

1

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.

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.