Magento -从购物车中删除一个数量

14ifxucb  于 2022-11-12  发布在  其他
关注(0)|答案(3)|浏览(145)

我试图删除只有一个数量从我的购物车,而不是所有,但无济于事。
有人能帮忙吗?
这是我到目前为止得到的代码...

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();
    }
}

UPDATE:下面是工作的代码,感谢R.S.!

$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;
    }
}
qxsslcnc

qxsslcnc1#

尝试

$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/

nfg76nw0

nfg76nw02#

您可以使用$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 {                   
}
f0brbegy

f0brbegy3#

对于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}}
   }
}

相关问题