Question

I have an problem retrieving specific discount information for an item of ordered with multiple qty.

Here's the scenario.

  1. An item with a price of $100.
  2. A shopping cart price rule of $10 off that item, limited to a quantity of 2.
  3. An order with a qty of 3 for the item (with the above rule applied).

For the order data, the item appears on one row like this:

  • Original Price: $100
  • Qty: 3 Subtotal: $300
  • Discount Amount: $20
  • Row Total: $280

Is there any way to retrieve the total quantity of items that the rule was applied to. In this case, it would be 2 (since the rule was limited to qty of 2). I know that Magento stores the applied_rule_ids, however using this cannot be a reliable way of retrieving the discount data applied to individual items because the Shopping Cart Price rules can be modified after the order has been placed.

It seems like Magento doesn't tell you where exactly discounts are being applied on the quantity level. It just groups multiple quantities into a single row and tells you overall how much was discounted.

Is it possible to retrieve that one item like this:

  • Name: Item Name | Discount Amount: $10 (discount applied)
  • Name: Item Name | Discount Amount: $10 (discount applied)
  • Name: Item Name | Discount Amount: 0 (discount NOT applied)

Instead of how it is now:

  • Name: Item Name | Discount Amount: $20 (discount applied to 2 out of 3)
Was it helpful?

Solution

As Fabian said, Magento does not save much information about what discounts have been applied how. In particular take a look at Mage_SalesRule_Model_Validator

The only information that is set is:

$item->setDiscountAmount($discountAmount);
$item->setBaseDiscountAmount($baseDiscountAmount);

$item->setOriginalDiscountAmount($originalDiscountAmount);
$item->setBaseOriginalDiscountAmount($baseOriginalDiscountAmount);
...
$item->setAppliedRuleIds(join(',',$appliedRuleIds));

One avenue worth exploring is this event:

Mage::dispatchEvent('salesrule_validator_process', array(
    'rule'    => $rule,
    'item'    => $item,
    'address' => $address,
    'quote'   => $quote,
    'qty'     => $qty,
    'result'  => $result,
));

since it has qty available which in your case should be the desired quantity. With the additional $item also being available you can add a custom column to the quote_item (and subsequently the order_item) to save this information. The big gotcha here is that one item can take part in multiple rules, so you would have to save the qty split by $rule->getId().

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top