0

I am using the following code to add products to Quote object which throws an error if the product is out of stock.

/** @var \Magento\Catalog\Model\Product $product */
$product->setTaxClassId($this->getTaxClassId())
        ->setPrice($price)
        ->setSpecialPrice($price)
        ->setTierPrice([])
        ->setBasePrice($baseprice)
        ->setOriginalCustomPrice($price)
        ->setRowTotal($rowTotal)
        ->setBaseRowTotal($rowTotal);
/** @var \Magento\Quote\Model\Quote $quote */
$quote->addProduct($product, (int)$qty);

How do I override the product qty check and create a backorder for each case ?

8
  • 1
    First, You do not need any customization for this required.if just need to enable back order setting at product inventory section. Open a product at admin, From Inventory section ,make backorder field value to Allow Qty Below 0 and Notify Customer .
    – Amit Bera
    Commented Jul 8, 2019 at 10:27
  • @AmitBera I am well aware of the settings available in configuration, but I need to override it irrespective of Allow Qty Below 0 and Notify Customer . Commented Jul 8, 2019 at 10:29
  • why you donot want use this setting?
    – Amit Bera
    Commented Jul 8, 2019 at 10:32
  • It will enable the backorder for the complete Website, but I need it only while creating order programmatically. Commented Jul 8, 2019 at 10:34
  • You can enable also for specific product wise from the product level. Open a product at admin and Open a product at admin, From Inventory section , make backorder field value to Allow Qty Below 0 and Notify Customer
    – Amit Bera
    Commented Jul 8, 2019 at 10:38

2 Answers 2

1

My other question solved the problem. What does “setIsSuperMode” do on Quote Object in Magento 2?

/** @var \Magento\Catalog\Model\Product $product */
$product->setTaxClassId($this->getTaxClassId())
        ->setPrice($price)
        ->setSpecialPrice($price)
        ->setTierPrice([])
        ->setBasePrice($baseprice)
        ->setOriginalCustomPrice($price)
        ->setRowTotal($rowTotal)
        ->setBaseRowTotal($rowTotal);

/** @var \Magento\Quote\Model\Quote $quote */
$quote->setIsSuperMode(true);
$quote->addProduct($product, (int)$qty);

Essentially it is a flag on the quote that this quote has been created by an admin. The effect of this is in the locations you stated that an admin can, for example, create an order for an item that normally would be out of stock or is not visible in the catalog.

2
  • 1
    I am aware of not visible in the catalog but not aware of the stock check. Good answer.
    – Amit Bera
    Commented Jul 9, 2019 at 7:32
  • Hello @Milind i have try same solution for my order that created programmatically , but not working from my side , can you please suggest Commented Dec 31, 2021 at 6:30
0

you can get the product stock and put the condition on that, here is a code for getting product stock.

<?php
namespace vendor\module\Block;
class ProductStock extends \Magento\Framework\View\Element\Template
{    
    protected $_stockItemRepository;

    public function __construct(
        \Magento\Backend\Block\Template\Context $context,        
        \Magento\CatalogInventory\Model\Stock\StockItemRepository $stockItemRepository,
        array $data = []
    )
    {
        $this->_stockItemRepository = $stockItemRepository;
        parent::__construct($context, $data);
    }

    public function getStockItem($productId)
    {
        return $this->_stockItemRepository->get($productId);
    }
}
?>

<?php
    $id = 123;
    $_productStock = $block->getStockItem($id);
    //print_r($_productStock->getData()); 
    echo $_productStock->getQty(); echo '<br />';
    echo $_productStock->getMinQty(); echo '<br />';
    echo $_productStock->getMinSaleQty(); echo '<br />';
    echo $_productStock->getMaxSaleQty(); echo '<br />';
    echo $_productStock->getIsInStock(); echo '<br />';
?>
2
  • Yes, I already know that, But I have a different question here. How do I create a Backorder If the product is out of stock, and programmatically? Commented Jul 8, 2019 at 10:37
  • then you have to make product manage stock to "No" or programmatically add the quantity and make a product in stock
    – nikunj
    Commented Jul 8, 2019 at 10:39

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.