How to set Dynamic Discount on each Cart Item programmatically?

How to set Dynamic Discount on each Cart Item programmatically?

September 28, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How to set Dynamic Discount on each Cart Item programmatically?

Setting dynamic discounts on each cart item programmatically involves understanding Magento’s core discount functionality and manipulating it through the appropriate classes and methods. Let me guide you through the process.

Understanding Magento Discounts

In Magento, discounts are primarily managed by the Mage_Sales_Model_Quote_Address class. This class contains two main instances: one for the billing address type and another for the shipping address type. The latter is mainly used for discounts related to coupon codes.

For your requirement—setting a $10 discount per item after a custom button press—the shipping address type instance of Mage_Sales_Model_Quote_Address is the primary target, as it allows you to add and manage discounts programmatically without necessarily involving coupon codes.

Setting Up the Discount

You’re on the right track with your initial code. To set up a discount for each cart item, follow these steps:

  1. Retrieve the cart object:
$cart = Mage::getSingleton('checkout/cart');
  1. Access the shipping address instance of Mage_Sales_Model_Quote_Address:
$objShippingAddress = $cart->getQuote()->getShippingAddress();
  1. Define the discount amount for each item ($10 in your case):
$discountAmount = 10;
  1. Set the discount description and add the total discount to the shipping address object:
$objShippingAddress->setDiscountDescription('Custom Line Item Discount');
$objShippingAddress->addTotal(array(
    'code' => 'line_item_discount',
    'title' => 'Line Item Discount',
    'value' => -$discountAmount * $cart->getItemsCount(),
));

Notice that I’ve multiplied the discount amount by the number of items in the cart to apply a $10 discount to each item. The code attribute is set to 'line_item_discount' to distinguish it from other types of discounts.

  1. Update the grand total and base grand total after applying the discount:
$objShippingAddress->setGrandTotal($objShippingAddress->getGrandTotal() - $objShippingAddress->getDiscountAmount());
$objShippingAddress->setBaseGrandTotal($objShippingAddress->getBaseGrandTotal() - $objShippingAddress->getBaseDiscountAmount());

Updating the Discount

To ensure that the discount is applied after a custom button press, you need to create an observer for the specific event that triggers this action. For example, if your custom button is part of the checkout page, you can create an observer for the checkout_cart_add_product or checkout_controller_onepage_save_shipping_method events.

In your observer method:

  1. Retrieve the cart and shipping address objects as shown earlier.
  2. Update the discount amount using the same logic as before (multiply by the number of items).
  3. Call the save() method on the shipping address object to save the changes:
$objShippingAddress->save();

Displaying the Discount

The issue you’re facing regarding the lack of a line in the “totals” section and order review section is likely due to caching or a misconfigured layout. Ensure that your Observer class is properly configured with the appropriate event listeners.

To display the discount in the totals section, update your checkout cart template (checkout/onepage/review/totals.phtml) to include the newly added line_item_discount:

<?php if ($this->getQuote()->getShippingAddress()->getTotal('line_item_discount') != 0): ?>
    <tr>
        <th><?php echo $this->__('Line Item Discount'); ?></th>
        <td>
            <?php echo $this->helper('core')->currency($this->getQuote()->getShippingAddress()->getTotal('line_item_discount'), true, false); ?>
        </td>
    </tr>
<?php endif; ?>

This code snippet checks if the line item discount is not zero and then displays it in the totals section.

Conclusion

Setting dynamic discounts on each cart item programmatically requires a thorough understanding of Magento’s core discount functionality. By targeting the shipping address instance of Mage_Sales_Model_Quote_Address and manipulating its properties, you can apply custom discounts to individual items without relying on coupon codes.

Remember to create an Observer class for your specific event trigger (custom button press) and update the layout accordingly to display the new discount in the totals section. If you’re still experiencing issues, consider reviewing your layout configuration or checking if any caching mechanisms are interfering with the display of the updated discounts.

For further reference on Magento development best practices and advanced customization techniques, visit LaravelCompany.

Checkout Configuration Pricing JavaScript

Our network