如何获得购物车中儿童产品的正确数量

How to get correct quantity for child product in cart

我使用以下代码获取购物车商品信息:

$cart_items = Mage::getSingleton('checkout/session')->getQuote()->getAllItems();
foreach( $cart_items as $items )
{
        $items->getQty();
}

在上面的代码中 $items->getQty() 始终 return "float(1)" 而超过 1 个数量的儿童产品添加到购物车中。

如何获取儿童产品的正确数量?

提前致谢。

对于简单的产品,您的代码应该可以工作。 尝试使用模型调用,看看它是否有所作为。还要检查 sales_flat_quote table 并查看 sales_flat_quote_item table 下找到的 items/quantity 以及它们是否与前端显示不匹配。

$oQuote = Mage::getModel( 'checkout/cart' )->getQuote();

// For all items.
$iTotalItemQty = $oQuote->getItemsQty();
echo $iTotalItemQty;

您是否也看到这种情况出现在数量 > 1 的简单产品或不同的产品类型中?

您是否尝试过 getAllVisibleItems() 而不是 getAllItems()

$cart_items = Mage::getSingleton('checkout/session')->getQuote()->getAllVisibleItems();
foreach( $cart_items as $items )
{
    $items->getQty();
}

最后我找到了我的解决方案:

$cart_items = Mage::getSingleton('checkout/session')->getQuote()->getAllItems();
foreach( $cart_items as $items )
{
    STATIC $qty='';
    if($items->getProductType() == 'configurable')  //configurable products
    {
        $qty = $items->getQty();
        continue;
    }
    else                                            // non-configurable product
    {
        if (!$items->getParentItem())               // product which has not parent product 
        {
            $qty = $items->getQty();
        }
    }
    echo $qty;
}