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 ?

  • 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 Jul 8 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 . – Milind Singh Jul 8 at 10:29
  • why you donot want use this setting? – Amit Bera Jul 8 at 10:32
  • It will enable the backorder for the complete Website, but I need it only while creating order programmatically. – Milind Singh Jul 8 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 Jul 8 at 10:38
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.

  • 1
    I am aware of not visible in the catalog but not aware of the stock check. Good answer. – Amit Bera Jul 9 at 7:32
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 />';
?>
  • 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? – Milind Singh Jul 8 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 Jul 8 at 10:39

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.