Magento 获取产品库存数量

例如要在产品页获取产品的库存数量:

$stock = Mage::getModel(‘cataloginventory/stock_item’)->loadByProduct($_product);

相关数量获取:

echo $stock->getQty();
echo $stock->getMinQty();
echo $stock->getMinSaleQty();


如果是可配置产品,获取产品库存数量总数:

if($_product -> isConfigurable ()){
$_associatedProducts = $_product -> getTypeInstance ( true )-> getUsedProductCollection ( $_product );
foreach ( $_associatedProducts as $p ) {
$productQty = (int) $p -> _data[‘stock_item’] -> _data[‘qty’] ;
$totalQty += $productQty ;
}
$qty = $totalQty;
}

相关文章