1

我试图从我的购物车中只删除一个数量而不是全部,但无济于事。

有人可以帮忙吗?

这是我到目前为止得到的代码......

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;
    }
}
4

3 回答 3

4

尝试

$cartHelper = Mage::helper('checkout/cart');
$items = $cartHelper->getCart()->getItems();
foreach ($items as $item) {
    if ($item->getProduct()->getId() == $yourProId) {
        if( $item->getQty() == 1 ){
            $cartHelper->getCart()->removeItem($item->getItemId())->save();
        }
        else if($item->getQty() > 1){
            $item->setQty($item->getQty() - 1)
            $cartHelper->getCart()->save();
        }
        break;
    }
}

看看@/app/code/core/Mage/Checkout/controllers/CartController.php

请参阅http://www.magentocommerce.com/boards/viewthread/30113/

于 2012-11-15T13:27:40.053 回答
0

您可以使用 $quoteItem->setData('qty', $avl_qty); 更改或删除数量 请参阅代码以获取更多帮助。

$quoteItem = $observer->getEvent()->getQuoteItem();
$avl_qty = 1;
if ($avl_qty == '0') {
  $quoteItem->getQuote()->removeItem($quoteItem->getItemId());
  throw new LocalizedException(__("This product is currently out of stock."));                    
} 
elseif ($order_qty > $avl_qty) {                   
  $quoteItem->setData('qty', $avl_qty);
  $this->_cart->save();
  $this->messageManager->addNoticeMessage('Sorry we have only '.$avl_qty.' qty of this product available');                    
} 
else {                   
} 
于 2017-04-05T06:09:27.683 回答
0

对于 Magento 2,您可以使用 带有身份验证令牌和正文的https://yourdomain.com/rest/V1/carts/mine/items API

{
   "cartItem": {
       "item_id": 49388,//item_id not SKU
       "qty": 1,        //This will overwrite quantity
       "quote_id": {{QuoteId}}
   }
}
于 2022-01-03T16:20:13.053 回答