Magento 2: How to decrements item qty?

I want to decrements item qty programmatically.

    $item = $this->cart->getQuote()->getItemById($itemId);
    $qty = $item->getQty();

    if($qty > 0) {
        $newqty = ($qty - 1);
        $item->setQty($newqty);
        $item->getProduct()->setIsSuperMode(true);
        $item->save();
        $this->cart->save();
    }

Please help me in this.

Solutions

Try with this code:

$item = $this->cart->getQuote()->getItemById($itemId);
if($item){
   $qty = $item->getQty();  
    if($qty > 2) {
        $newqty = ($qty - 1);
        $item->setQty($newqty);
        $item->save();
    }    
}
$this->cart->getQuote()->save();

Also, you have to Update Mini Cart using sections.xml When you hitting this above code.

See more how to apply Update Mini Cart https://webkul.com/blog/update-mini-cart-magento2/

Similar questions

Magento order with parent item and child item instead of only child item
i recently discovered a issue with some of our orders. We have configuable products with simple products as childs, selectable via one attribute. Example: The sizes are the attribute to select in the configurable product. Until last week, when a customer chosed a child product, let's say #972, the order information in the backend showed this child ...
How do I set enable qty increments and set the qty increment in php
I have a large list of products that for each I have to enable qty_increments and set the qty_ increment to a specific value for that product. I am not sure how to do that in php. I know how to get and save the product just not how do enable and set the increments my input is: sku qty_increment sku qty_increment .... Thanks
Product status not changing when qty goes below minimum qty
I am using magento-1.6.1.0 When qty of poduct goes below minimum qty still it show product "In stock". I have set everything prodperly on system > config > tab. still facing the same issue. Thanks in advance.
Stock qty is not updating after order place with custom qty
I am trying to set stock qty of products with my custom stock qty after placing order. So I used following code in my config.xml file- Obeserver.php file - now if product with id 12345 has current stock qty 4 and an order with qty 1 of this product is placed then I want its qty should be 5 or whatever I want. but currently it is decreasing and show...
Prevent adding to cart a product in "non multiple" qty when qty Increments enabled
Is there any easy way to show a warning in the product page and prevent adding product to cart instead of showing the message in cart page, when the quantity increments entered in quantity input field are incorrect? Thanks
How to add available qty to cart when customer request more than available qty?
As asked in the question, Is there any possibility to add available qty(in stock) to the cart when customer requesting more than available qty. How magento behave with this situation? ex : I have 5 qty in product A, Customer requesting 10. I want to add the available 5 to the cart and show a message to customer.

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.