简体   繁体   English

Magento2.4:如何在 list.phtml 文件中获取可销售数量?

[英]Magento2.4: How to get Salable quantity in list.phtml file?

How can I get salable quantity on list.phtml or category page file, I want to show labels on products with 0 salable quantity.如何在 list.phtml 或类别页面文件上获取可销售数量,我想在可销售数量为 0 的产品上显示标签。 Are there any other approaches without using object manager?不使用对象管理器还有其他方法吗?

Please use this code in phtml file to get salable qty请在 phtml 文件中使用此代码以获取可销售数量

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

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);
?>

Either Object Manager is not a good approach, but you need to inject in your custom module such as:对象管理器不是一个好方法,但您需要注入您的自定义模块,例如:

namespace Cloudways\Module\ModelName;
use Magento\InventorySalesAdminUi\Model\GetSalableQuantityDataBySku;

Refer Link: https://magento.stackexchange.com/questions/301956/how-to-get-salable-qty-in-magento-2-3-3/302187#302187参考链接: https ://magento.stackexchange.com/questions/301956/how-to-get-salable-qty-in-magento-2-3-3/302187#302187

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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