Remove quantity of item in cart
-
12-05-2021 - |
题
The Problem
I'm looking at removing a specific quantity of an item that I already know is within the cart. There are lots of guides explaining how to remove items completely from the cart but let's say I have an item in the cart with a quantity of 2 I just want to remove a quantity of 1 rather than the product itself.
解决方案
Assuming you already have the item you want to change just do this:
$item->setQty( $item->getQty() - $qtyToRemove);
if not you have to iterate throught the products in your cart, something like this should work:
foreach($cart->getQuote()->getAllVisibleItems() as $item)
{
if $item->getProduct()->getId() == your_productid)
{
$item->setQty( $item->getQty() - $qtyToRemove);
}
}
$this->_cart->save();
let me knwo if you need any further assistance :)