获取购物车中每种产品的产品数量[关闭]
-
12-12-2019 - |
题
$cart = Mage::getModel('checkout/cart')->getQuote();
$cartTotal =$cart->getGrandTotal();
$cartItems = $cart->getAllItems();
foreach ($cartItems as $item)
{
$_product = $item->getProduct();
$id=$_product->getId();
$qty = $_product->getQty(); //get product Qty
$image=$_product->getImageUrl();
}
解决方案
瓦巴哈夫,请使用 getAllVisibleItems
反而 getAllItems
要获取购物车商品的数量,您需要使用 getQty()
$cart = Mage::getModel('checkout/cart')->getQuote();
$cartTotal =$cart->getGrandTotal();
$cartItems = $cart->getAllVisibleItems();
foreach ($cartItems as $item)
{
$_product = $item->getProduct();
$id=$_product->getId();
$qty = $item->getQty(); //get item qty
}
为了得到 current product qty
,你需要加载 product model by sku
然后得到 inventory
从库存对象
$_producObject = Mage::getModel('catalog/product')->loadByAttribute('sku',$item->getSku());
$stock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($_producObject);
echo "<pre>";
print_r($stock->getData());
echo "</pre>";