本文介绍了Magento-从购物车中取出一个数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to remove just one qty from my cart rather than all, but to no avail.

有人可以帮忙吗?

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

Here is the code I have got so far...

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,有效的代码

UPDATE: Here is the code that works, thanks to R.S. for this!

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


推荐答案

尝试

$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 /法师/结帐/控制器/CartController.php

Take a look @ /app/code/core/Mage/Checkout/controllers/CartController.php

请参见

这篇关于Magento-从购物车中取出一个数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 01:52