Discount information for order in Magento
题
I need information which promotion/coupon changed which item in order and what was the price of this item before discount and the final price of this item. I know, it's complicated so here's an example:
<order>
<items>
<item> <!-- This item have discount in order -->
<sku>1234</sku>
<promotion>
<promo_id>456</promo_id>
<discount_value>10</discount_value><!-- In % or $ -->
</promotion>
<final_price>25</final_price>
</item>
<item><!-- This item don't have discount in order -->
<sku>1234</sku>
<promotion/>
<final_price>35</final_price>
</item>
</items>
</order>
I hope it's comprehensible explanation. Thanks for all help.
EDIT: I forgot about one thing. All i have it's information from sales/order model. It's an interface, so i don't have access to session data, I get validated orders from database.
解决方案
You can easily retrieve applied shopping cart rule ids from order item model.
You need to walk-though order items to collect all applied rule ids to items:
$collectedRuleIds = array(); $itemRules = array(); foreach ($order->getAllVisibleItems() as $orderItem) { if ($orderItem->getAppliedRuleIds()) { $itemRules[$orderItem->getId()] = explode(',', $orderItem->getAppliedRuleIds()); $collectedRuleIds = array_merge($collectedRuleIds, $itemRules[$orderItem->getId()]); } }
Then load rules collection, to retrieve rule information and assign it to order items.
$rules = false; if ($collectedRuleIds) { $rules = Mage::getModel('salesrule/rule')->getCollection()->addFieldToFilter('rule_id', array('in' => $collectedRuleIds)); } if ($rules) { foreach ($itemRules as $itemId => $ruleIds) { $orderItem = $order->getItemById($itemId); $appliedRules = array(); foreach ($ruleIds as $ruleId) { if ($rules->getItemById($ruleId)) { $appliedRules[] = $rules->getItemById($ruleId); } } $orderItem->setAppliedRules($appliedRules); } }
During exporting data, just check getAppliedRules property of order item to get information about type of promotion for building proper export file.