Magento – How to add out of stock product to cart programmatically in Magento 2.3

addtocartmagento2magento2.3out-of-stockquote

I'm using Magento v2.3, I want to add out of stock products to cart without changing any configuration from backend.

I'm using following function to add product to cart

addProduct($productInfo, $requestInfo)

from following class

Magento\Checkout\Model\Cart

But it checks for saleable quantity.

Is there any way to ignore saleable check and add out of stock products to cart?

Best Answer

If you want to add out of stock products to cart without changing any configuration from the backend then you have added enable super mode of the product during add to card

    $productInfo->setIsSuperMode(true) ; 

...->addProduct($productInfo, $requestInfo);