2

我有一个产品的产品 ID。如果它已经在购物车中(如果它存在用于登录用户),我想从购物车中获取该产品的数量。

如果用户(注册)用户将产品添加到购物车离开站点。然后在某个时候他登录回站点并将相同的产品添加到购物车。此时我想检查购物车中是否有相同的产品以及什么是产品的数量?

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

foreach ($cart_m->getAllItems() as $item)
{
    $cart_productId = $item->getProduct()->getId();
    $productPrice = $item->getProduct()->getPrice();
    $productQuantity = $item->getProduct()->getQty();//I want to get specific product quantity,If I have product ID

}
4

2 回答 2

3
$cart_m = Mage::getModel('checkout/cart')->getQuote();
$yourProductId = 10;
$needQty = 0; 
foreach ($cart_m->getAllItems() as $item)
{

    $cart_productId = $item->getProduct()->getId();
    $productPrice = $item->getProduct()->getPrice();
    $productQuantity = $item->getProduct()->getQty();

    if ($yourProductId == $cart_productId){
        $needQty = $productQuantity;
        break;
    }
}

if (!$needQty){
    echo "This Product Not In Cart";
}else{
    echo $needQty;
}
于 2013-02-28T11:13:08.547 回答
1

您可以在不循环所有项目的情况下获取产品数量:

$quote = Mage::getSingleton('checkout/session')->getQuote();
$_item = $quote->getItemByProduct($product);
$qty = $_item->getQty();
于 2015-12-23T13:04:01.780 回答