title: Magento: Update cart quantity programmatically
body: <p>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.</p>
  
  <pre><code>$quote = Mage::getSingleton('checkout/session')-&gt;getQuote();
  $cartItems = $quote-&gt;getAllVisibleItems();
  foreach ($cartItems as $item) {
    //Do something
  }
  </code></pre>
  
  <p>What i want is to update the quantity on cart for a specific product. I know it can be done like this</p>
  
  <pre><code>$pid=15;
  $quote = Mage::getSingleton('checkout/session')-&gt;getQuote();
  $cartItems = $quote-&gt;getAllVisibleItems();
  foreach ($cartItems as $item) {
  if($pid==$item-&gt;getId())
  $item-&gt;setQty($qty);
  }
  </code></pre>
  
  <p>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.</p>