title: How to programmatically apply a magento sales rule to an quote item
body: <p>I'm currently implementing a plugin to manage quotation requests in a magento shop system. The idea is, that the request is associated with an quote and somebody in the backend should be able to apply individual sales rules per quote item.</p>
  
  <p>There was no problem to implement the functionality to create and edit the involved entities on the frontend as well in backend. But I'm unable to apply a certain sales-rule to a quote item and have that rule reflected in the row total as well as the subtotal of the quote.</p>
  
  <p>My current approach is to use the <code>sales_quote_address_discount_item</code>-Hook. There I use an instance of a class derived from <code>Mage_SalesRule_Model_Validator</code> which overloaded <code>_getRules()</code>-Method gives the relevant <code>Mage_SalesRule_Model_Rule</code>s. The relevant code of the event-observer looks like:</p>
  
  <pre><code>$quote_item = $event-&gt;getItem();
  
  $request_item = $this-&gt;helper-&gt;getRequestItemByQuoteItemId($quote_item-&gt;getItemId());
  if (! $this-&gt;isRelevantRequestItem($request_item))
      return $this;
  
  $validator = Mage::getModel("requestquotation/request_validator")
                   -&gt;addRule($request_item-&gt;getRule());
  $validator-&gt;process($quote_item);
  </code></pre>
  
  <p>When I step with the debugger through <code>process()</code>-Method, my supplied rule works and is applied to the quote item. But any further <code>collectTotals()</code>on the quote and an <code>$quote_item-&gt;save()</code> has no effects on the of row-totals and the subtotal of the quote.</p>
  
  <p>Is there any documentation or example how to add an sales-rule (better in my opinion as it is traceable) or an discount manually, programatically to an quote-item and/or to the quote itself.</p>
  
  <p>Thank you in advance and best regards!</p>
  
  <p>Joachim</p>