문제

How to get salable qty of each Product and display in Product view page.

Can someone help me?

Thanks in advance.

도움이 되었습니까?

해결책

<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$StockState = $objectManager->get('\Magento\InventorySalesAdminUi\Model\GetSalableQuantityDataBySku');
$qty = $StockState->execute($_product->getSku());
echo ($qty[0]['qty']);
?>

You can get only the saleable quantity by the following above code.

다른 팁

Try the below code to get salable QTY.

<?php 
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $StockState = $objectManager->get('\Magento\InventorySalesApi\Api\GetProductSalableQtyInterface');
    $qty = $StockState->execute($_product->getSku(), 2);
?>

Hope this will help you.

Use the following code :-

namespace Vendor\Module\ModelName;
use Magento\InventorySalesAdminUi\Model\GetSalableQuantityDataBySku;
class ClassName
{
    private $getSalableQuantityDataBySku;
    public function __construct(
        GetSalableQuantityDataBySku $getSalableQuantityDataBySku
    ) {
        $this->getSalableQuantityDataBySku = $getSalableQuantityDataBySku;
    }
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $sku = "testsku"; //pass your product sku
        $salable = $this->getSalableQuantityDataBySku->execute($sku);
        echo json_encode($salable);
    }
}

Output :-

[{"stock_name":"Default Stock","qty":4,"manage_stock":true}]

Reference Link

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 magento.stackexchange
scroll top