In sales_flat_quote_item and sales_flat_quote tables, discount_amount and base_discount_amount fields give you the discount amount on each item of your cart/order.
However, if you have many salesrule on your item, you can’t have the discount for each rule.

Avoid the rewrite !

We are lucky ! In Magento, there is an observer which return the quote item, with the calculcated discount amount for one rule.
The observer is : salesrule_validator_process
It is called on each salesrule applied on the item.

        <events>
            <salesrule_validator_process>
                <observers>
                    <minimax_salesrule_validator_process>
                        <class>minimax_discountdetails/observer</class>
                        <method>setDiscountAmountByRule</method>
                    </minimax_salesrule_validator_process>
                </observers>
            </salesrule_validator_process>
        </events>

Organize our datas

So, we have our observer with our datas. We will built an array with this structure :

$discountDetails[$ruleId] = array(
'discount_amount' => round($discountAmount,2),
'base_discount_amount' => round($discountAmount,2),
);

This array is populated for each item, so we can find the discount amount by salesrule and by item.
In order to save the datas on database, I decided to serialize it. Don’t forget to un sererialize it when you retrieve it !

Convert to Order Item

Now, we have the information on sales_flat_quote_item table. We need to send it on the sales_flat_order_item table :

        <fieldsets>
            <sales_convert_quote_item>
                <discount_details>
                    <to_order_item>*</to_order_item>
                </discount_details>
            </sales_convert_quote_item>
            <sales_convert_order_item>
                <discount_details>
                    <to_quote_item>*</to_quote_item>
                </discount_details>
            </sales_convert_order_item>
        </fieldsets>

Conclusion

Et voilà ! We added a little but smart functionnality on Magento !
The informations “disount_details” is not used in this module, it’s just saved.
You can get it on your order_item or your quote_item with the magical getter : getDiscountDetails().
As I said before : Don’t forget to unserialize it !

Download

Download the module with the link below.
After copy and paste the files, don’t forget to clean all caches !
I advice to delete also all folders in var/cache manually.
If you have any suggestion or bug report, post a comment in this post 😉
Download Module

Save item’s discount amount per coupon/rule in Magento

One thought on “Save item’s discount amount per coupon/rule in Magento

  • 12/17/2017 at 15:29
    Permalink

    this module is showing in my promotions rule action
    at where is maximum discount need set

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

We use cookies to ensure that we give you the best experience on our website.
Ok