title: magento coupon code is not valid
body: <p>I am using magento 1.7.0.2. For some reason, I keep getting "Coupon code "XXX" is not valid." I investigated a little bit, and found what the problem is but I don't know how to fix it.</p>
  
  <p>in the file: \app\code\core\mage\checkout\controllers\cartController.php</p>
  
  <pre><code>$couponCode = (string) $this-&gt;getRequest()-&gt;getParam('coupon_code');
  if ($this-&gt;getRequest()-&gt;getParam('remove') == 1) {
      $couponCode = '';
  }
  $oldCouponCode = $this-&gt;_getQuote()-&gt;getCouponCode();
  
  if (!strlen($couponCode) &amp;&amp; !strlen($oldCouponCode)) {
      $this-&gt;_goBack();
      return;
  }
  
  try {
      $this-&gt;_getQuote()-&gt;getShippingAddress()-&gt;setCollectShippingRates(true);
      $this-&gt;_getQuote()-&gt;setCouponCode(strlen($couponCode) ? $couponCode : '')
      -&gt;collectTotals()
      -&gt;save();
  
      if ($couponCode) {
          if ($couponCode == $this-&gt;_getQuote()-&gt;getCouponCode()) {
              $this-&gt;_getSession()-&gt;addSuccess(
                  $this-&gt;__('Coupon code "%s" was applied.',Mage::helper('core')-&gt;htmlEscape($couponCode))
                  );
          }
          else {
              $this-&gt;_getSession()-&gt;addError(
              $this-&gt;__('Coupon code "%s" is not valid.', Mage::helper('core')-&gt;htmlEscape($couponCode))
                  );
          }
      } else {
          $this-&gt;_getSession()-&gt;addSuccess($this-&gt;__('Coupon code was canceled.'));
      }
  </code></pre>
  
  <p>The problem is that <code>$this-&gt;_getQuote()-&gt;getCouponCode()</code> comes empty. It's coming in as ''.</p>
  
  <p>Edit:</p>
  
  <p>A further investigation led me to the a more specific problem..</p>
  
  <p>Its the <code>-&gt;collectTotals()-&gt;save();</code> that doing all the mess..
  For some reason if i remove them it runs perfectly but the coupon doesn't apply.</p>
  
  <p>How can this be fixed?</p>