我试图从我的购物车中只删除一个数量而不是全部,但无济于事。
有人可以帮忙吗?
这是我到目前为止得到的代码......
require_once 'app/Mage.php';
Mage::app("default");
Mage::getSingleton("core/session", array("name" => "frontend"));
$session = Mage::getSingleton("customer/session");
$yourProId = $_POST['prodID'];
$qty = 1;
foreach (Mage::getSingleton('checkout/session')->getQuote()->getItemsCollection() as $item) {
if ($yourProId == $item->getProductId()) {
Mage::getSingleton('checkout/cart')->removeItem($item->getId())->save();
}
}
更新:这是有效的代码,感谢RS!
$yourProId = $_POST['prodID'];
$qty=1;
$cartHelper = Mage::helper('checkout/cart');
$items = $cartHelper->getCart()->getItems();
foreach ($items as $item) {
if ($item->getProduct()->getId() == $yourProId) {
$qty = $item->getQty() - 1; // check if greater then 0 or set it to what you want
if($qty == 0) {
Mage::getSingleton('checkout/cart')->removeItem($item->getId());
} else {
$item->setQty($qty);
}
$cartHelper->getCart()->save();
break;
}
}