简体   繁体   中英

How to get backorder status from product information programmatically in Magento 2

How to get backorder status from product information programmatically?

Now the product information is ready as $_product , also we can get inventory status by this code.

<?php $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
     $StockState = $objectManager->get('\Magento\CatalogInventory\Api\StockStateInterface');
     $product_is_stock = $StockState->getStockQty($_product->getId(), $_product->getStore()->getWebsiteId()); ?>

Also, backorder feature was settled so we can add the out stock products on cart.

I had tried to get result by full searching whole channels, but I didn't get proper solution.

After debugging and investigating vendor API structures on several hours, I got the correct answers myself. This is proved answers on Magento 2.1.5, 2.1.7 .

$obj = \Magento\Framework\App\ObjectManager::getInstance();  
$stockRegistry = $obj->get('Magento\CatalogInventory\Api\StockRegistryInterface');
$stockitem = $stockRegistry->getStockItem($_product->getId(),$_product->getStore()->getWebsiteId());
echo "Backorder: "; echo $stockitem->getBackorders(); echo '<br>';

I added this part on

app\design\frontend\[vendor]\[theme]\Amasty_Cart\templates\rightside\cart.phtml 
app\design\frontend\[vendor]\[theme]\Magento_Checkout\templates\cart\form.phtml

which I should determine backorderable products status.

Finally, I could get 0, 1, 2 values according to the 3 status of backorders.

0: "No Backorders"
1: "Allow Qty Below 0 "
2: "Allow Qty Below 0 and Notify Customer"

This will help many developers who create new modules or customize themes with its efficiency.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM