我有产品的产品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
}
答案 0 :(得分: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;
}
答案 1 :(得分:1)
您可以在不循环所有项目的情况下获得产品数量:
$quote = Mage::getSingleton('checkout/session')->getQuote();
$_item = $quote->getItemByProduct($product);
$qty = $_item->getQty();