0

使用一个简单的产品,我可以做到这一点:

$quote = Mage::getSingleton('checkout/session')->getQuote();
$item = $quote->getItemById($params['item']); // $params['item'] contains item id
$product = $item->getProduct();
$stockQty = (int)Mage::getModel('cataloginventory/stock_item')->loadByProduct($product)->getQty();

但是,如果该项目是已配置产品,它会尝试获取其父可配置产品的库存,在大多数情况下,该产品的库存为零。

如何获得已配置产品的库存数量,最好不循环访问其可配置父级的子级产品?

4

1 回答 1

3

您可以设置一个变量,根据可配置产品的子项的库存状态确定库存状态:

$inStock = true;
$quote = Mage::getSingleton('checkout/session')->getQuote();
$cart = Mage::getModel('checkout/cart')->getQuote();

$item = $quote->getItemById($params['item']); // $params['item'] contains item id
$_product = $item->getProduct();
$configuredProduct = Mage::getModel('catalog/product_type_configurable')->setProduct($_product);
$children = $configuredProduct->getUsedProductCollection()->addAttributeToSelect("*")->addFilterByRequiredOptions();
foreach($children as $simpleProduct) {
    foreach ($cart->getAllItems() as $item) {
        if ($item->getProduct()->getId() != $simpleProduct->getId()) continue;

        $stockQty = (int)Mage::getModel('cataloginventory/stock_item')->loadByProduct($simpleProduct)->getQty();
        if ($stockQty <= 0) {
            $inStock = false;
            break;
        }
    }

}
于 2013-05-21T10:11:57.683 回答