0

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.

1
  • You can update the product as per changed quantity.
    – Amit Saini
    Commented May 11, 2021 at 12:46

1 Answer 1

1

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 :)

1
  • I had to save the cart in order for the above code to work but after that was added it works great. I will suggest an edit to the question to amend the extra line Commented May 11, 2021 at 14:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.