php Magento:以编程方式更新购物车数量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15152153/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Magento: Update cart quantity programmatically
提问by Nirmal Ram
I am creating a magento extension. In which I want to update the item quantity in cart programmatically. I am using the following code to display the items in cart.
我正在创建一个 magento 扩展。我想以编程方式更新购物车中的商品数量。我正在使用以下代码显示购物车中的商品。
$quote = Mage::getSingleton('checkout/session')->getQuote();
$cartItems = $quote->getAllVisibleItems();
foreach ($cartItems as $item) {
//Do something
}
What i want is to update the quantity on cart for a specific product. I know it can be done like this
我想要的是更新特定产品的购物车数量。我知道可以这样做
$pid=15;
$quote = Mage::getSingleton('checkout/session')->getQuote();
$cartItems = $quote->getAllVisibleItems();
foreach ($cartItems as $item) {
if($pid==$item->getId())
$item->setQty($qty);
}
But I don't like this method as it would go through each and every product to update the quantity of a single product. I wonder if there is a way to update the quantity in one line i:e without using for loop.
但是我不喜欢这种方法,因为它会遍历每个产品以更新单个产品的数量。我想知道是否有一种方法可以在不使用 for 循环的情况下更新一行 i:e 中的数量。
回答by dagfr
You have the product_id, not the item_id right ?
您有 product_id,而不是 item_id,对吗?
If it's the case it's impossible to get the product id without performing the whole items.
如果是这种情况,则不可能在不执行整个项目的情况下获得产品 ID。
Look at Mage_Sales_Model_Quote::getItemByProduct($product);you'll see it performs the whole catalog.
看看Mage_Sales_Model_Quote::getItemByProduct($product);你会看到它执行整个目录。
Basically I'll do it like this :
基本上我会这样做:
//get Product
$product = Mage::getModel('catalog/product')->load($pid);
//get Item
$item = $quote->getItemByProduct($product);
$quote->getCart()->updateItem(array($item->getId()=>array('qty'=>$qty)));
$quote->getCart()->save();
You should optimise this with some quick tricks :
您应该使用一些快速技巧来优化它:
$quote->hasProductId($pid) to check if product is in the cart
and
和
($qty!=$item->getQty())
to check if quantity is not already good...
检查数量是否还不够好......
Please note that it is untested code, and some kind of reflexions to help you find the solution, I didn't do the job for you.
请注意,这是未经测试的代码,以及一些帮助您找到解决方案的反思,我没有为您完成这项工作。
Kind Regards,
亲切的问候,
回答by Rajesh Jai
I am sure it will work. Try this. It's save's my time.
我相信它会起作用。尝试这个。这是节省我的时间。
public function updateCartAction(){
$item_id = $this->getRequest()->getParam('item_id');
$qty = $this->getRequest()->getParam('qty');
$quote = Mage::getSingleton('checkout/session')->getQuote();
$quote->updateItem($item_id, array( 'qty' => $qty));
$quote->save();
echo "Sssssss";
}
回答by SyraKozZ
another solution
另一种解决方案
$quote = Mage::getModel('sales/quote')->setStoreId($storeId)->load($cartid);
$cartItems = $quote->getAllItems();
foreach ($cartItems as $item) {
if($cartiemid==$item->getId()){
$item->setQty($qty);
$item->save(); // you need to save the item after qty update
}
}
$quote->save();

