title: Magento programmatically get discount giving coupon code and products
body: <p>I'm trying to integrate a system with Magento, I would like a way to send the coupon code, current user and cart to Magento and retrieve the according discount (if applies), so I don't have to replicate all the logic behind coupon validation.</p>
  
  <p>I would really appreciate it.</p>
  
  <p>I managed to do the following. </p>
  
  <pre><code>    $customerId = 1;
      $couponCode = "TESTCOUPON";
      $json = "{
                  \"cart\":[{
                      \"listProduct\":[{
                          \"idReferenceProduct\":15,
                          \"quantity\":1
                      }]
                  }]
              }";
      $jsonDecoded = json_decode($json);
      $products = $jsonDecoded-&gt;cart[0]-&gt;listProduct;
  
      // *********************************************************
  
      $customerObj = Mage::getModel('customer/customer')-&gt;load($customerId);
      $storeId = $customerObj-&gt;getStoreId();
      $quoteObj = Mage::getModel('sales/quote')-&gt;assignCustomer($customerObj);
      $storeObj = $quoteObj-&gt;getStore()-&gt;load($storeId);
      $quoteObj-&gt;setStore($storeObj); 
  
      foreach ($products as $singleProduct) {
  
          $productObj =  Mage::getModel('catalog/product');
          $productObj-&gt;load($singleProduct-&gt;idReferenceProduct);
          echo $productObj-&gt;getName();
          echo $productObj-&gt;getPrice();
  
          try{
              $quoteItem = $quoteObj-&gt;addProduct($productObj);
              $quoteItem-&gt;setPrice($productObj-&gt;getPrice());
              $quoteItem-&gt;setQty($singleProduct-&gt;quantity);
              $quoteItem-&gt;setQuote($quoteObj);                                    
              $quoteObj-&gt;addItem($quoteItem);
  
          } catch (exception $e) {
              echo "error creating quote item ";
          }
  
          $singleProduct-&gt;quantity);
      }
  
      try{
          $quoteObj-&gt;setCouponCode($couponCode); 
      }
      catch(exception $e){
          return "error setting coupon";
      }
  
      $quoteObj-&gt;collectTotals();
  
      var_dump($quoteObj-&gt;toArray());
  </code></pre>
  
  <p>And the output:</p>
  
  <pre><code>{
  ["customer_id"] = &gt; "1" 
  ["customer_prefix"] = &gt; NULL
  ["customer_firstname"] = &gt; "xxxxxx" 
  ["customer_middlename"] = &gt; NULL
  ["customer_lastname"] = &gt; "xxxxxxx" 
  ["customer_suffix"] = &gt; NULL
  ["customer_email"] = &gt; "xxxxxxxxxx@gmail.com" 
  ["customer_dob"] = &gt; "1981-03-06 00:00:00" 
  ["customer_taxvat"] = &gt; NULL
  ["customer_gender"] = &gt; "1" 
  ["customer_group_id"] = &gt; "1" 
  ["customer_tax_class_id"] = &gt; "3" 
  ["store_id"] = &gt; "1" 
  ["coupon_code"] = &gt; "TESTCOUPON"
  ["subtotal"] = &gt; float(872.06)
  ["base_subtotal"] = &gt; float(872.06)
  ["subtotal_with_discount"] = &gt; float(830.92)
  ["base_subtotal_with_discount"] = &gt; float(830.92)
  ["grand_total"] = &gt; float(929.64)
  ["base_grand_total"] = &gt; float(929.64)
  ["applied_rule_ids"] = &gt; "1" 
  ["virtual_items_qty"] = &gt; int(0)
  ["taxes_for_items"] = &gt; {
      [""] = &gt; {
          [0] = &gt; {
              ["rates"] = &gt; {
                  [0] = &gt; {
                      ["code"] = &gt; "IVA" 
                      ["title"] = &gt; "IVA" 
                      ["percent"] = &gt; float(12)
                      ["position"] = &gt; "1" 
                      ["priority"] = &gt; "1" 
                      ["rule_id"] = &gt; "1"
                  }
              }["percent"] = &gt; float(12)
              ["id"] = &gt; "IVA"
          }
      }
  }["items_count"] = &gt; int(2)
  ["items_qty"] = &gt; float(2)
  ["trigger_recollect"] = &gt; int(0)
  ["can_apply_msrp"] = &gt; bool(false)
  ["totals_collected_flag"] = &gt; bool(true)
  }
  </code></pre>
  
  <p>The coupon discount is supposed to be a 5% discount. </p>
  
  <p>For some reason the prices are not correct. That product price is 516.00 and the subtotal in the output states 872.06. Also there's only one item and the output states 2 items. Am I doing something wrong?</p>