How to programmatically apply a magenta sale rule to an offer item

I am currently implementing a quote request management plugin in the magento store system. The idea is that the request is associated with a quote, and someone from the backend should be able to apply individual sales rules in quotation marks.

There were no problems with the implementation of functionality for creating and editing the objects involved in the interface, as well as in the backend. But I can’t apply a specific sales rule to an offer item and include this rule in the total amount of the line, as well as the subtotal of the quote.

My current approach is to use sales_quote_address_discount_item -Hook. There I use an instance of a class derived from Mage_SalesRule_Model_Validator , which is overloaded with _getRules() -Method gives the corresponding Mage_SalesRule_Model_Rule s. The corresponding event observer code looks like this:

 $quote_item = $event->getItem(); $request_item = $this->helper->getRequestItemByQuoteItemId($quote_item->getItemId()); if (! $this->isRelevantRequestItem($request_item)) return $this; $validator = Mage::getModel("requestquotation/request_validator") ->addRule($request_item->getRule()); $validator->process($quote_item); 

When I step with the debugger through process() -Method, my posed rule works and applies to the sentence element. But any further collectTotals() in the quote and $quote_item->save() does not affect the totals of the strings and the subtotal of the quote.

Is there any documentation or an example of how to add a sales rule (better, in my opinion, traceable), or a manual discount, programmatically for a quotation item and / or for a quote.

Thanks in advance and best regards!

Joachim

+4
source share
2 answers

Unfortunately, Chuck is currently not doing consulting work. Therefore, I had to delve deeper into the intricate calculation of the magenta discount.

Recalculation by totals_collected_flag not IMHO without parameters, since it doubles the calculation of taxes, which means that the total amount of quotes is completely erroneous.

I have had success to fill out my own discount type by introducing the new Mage_Sales_Model_Quote_Address_Total_Abstract . This one uses the collect() method in it with a slightly customized version of Mage_SalesRule_Model_Validator , which loads custom sales rules in accordance with its discount strategy.

A custom class is included in Magento's summary calculation by adding the following XML to the module configuration.

 <global> ... <sales> <quote> <totals> <requestquotation_discount> <class>requestquotation/request_discount</class> <after>discount</after> <before>grand_total</before> </requestquotation_discount> </totals> </quote> </sales> </global> 

In the implementation, the "code" of the totals calculator is set to "discount", and no previous calculated discount is reset. Using this method, a custom discount can work side by side with the default discount system Magento, and also does not interfere with the calculation of taxes, etc.

Hope this helps someone deal with the same problems.

+10
source

Chuck Norris is the only one who has Magento documentation. Nevertheless, you can try to set the assembled flag to false before calling collectTotals ():

 $quote->setTotalsCollectedFlag(false)->collectTotals(); 

and optionally, if you end up with incorrect totals after calling collectTotals more than once in one instance of the object, then you need to know the following problem:

http://www.magentocommerce.com/bug-tracking/issue?issue=11893

+18
source

All Articles