How to set discount amount with discount code when create order programmatically?

How to set discount amount with discount code when create order programmatically?

August 24, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How to set discount amount with discount code when create order programmatically?

Setting Discount Amount with Discount Code for Programmatic Orders

When creating orders programmatically in Magento, you might face challenges when applying discounts correctly. One common issue is that the discount code is added to the order but shows as $0. In this post, we’ll guide you through solving this problem and properly setting the discounted price on products.

Understanding the Issue: Discount Code Showing Zero Amount

The core issue lies in how Magento processes discount codes during order creation. When you create an order programmatically and apply a discount code, Magento doesn’t always calculate the discount amount correctly on the product level. Instead, it applies the discount to the total order amount.

In your provided code, while you’re setting the setCouponCode("SALE") and calling collectTotals(), the discount isn’t being applied properly at the item level:

$quote->setCouponCode("SALE");
$quote->collectTotals()->save();

Solving the Problem: Applying Discounts to Individual Items

To fix this, you need to apply the discount directly to each item in your cart before submitting the order. Here’s how to modify your code:

foreach($salesReport as $ids => $qty)
{
    $product1 = Mage::getModel('catalog/product')->load($ids); /* HTC Touch Diamond */
    $buyInfo1 = array('qty' => $qty);

    // Add the product with a discount
    $quoteItem = $quote->addProduct($product1, new Varien_Object($buyInfo1));
    $discountAmount = 0.5; // Set your desired discount amount here

    // Apply the discount to this item
    if ($discountAmount > 0) {
        $quoteItem->setDiscountAmount($discountAmount);
        $quoteItem->save();
    }
}

In this updated code, we’re applying a fixed discount amount ($discountAmount = 0.5;) directly to each product item in the quote before saving it.

Best Practices for Discount Application

For better control and flexibility:

  1. Use Magento’s built-in discount rules: Instead of hard-coding discounts, consider creating discount rules via the admin interface or using extensions like Magento 2 Discount Rules to manage discounts programmatically.

  2. Validate and handle exceptions: Always validate if a discount is applicable before applying it to prevent errors during order submission.

  3. Monitor your order total: Ensure the calculated totals match your expectations to catch any discrepancies early.

Conclusion: Setting Discounts Correctly

Setting discount amounts with discount codes for programmatic orders involves understanding Magento’s discount processing and applying discounts at the correct level. By directly setting a discount on each item in the quote, you can ensure that both the item-level and order-level discounts are applied correctly.

Remember to test your setup thoroughly and monitor order totals to ensure everything works as expected. For more advanced discount management solutions, consider exploring extensions like Magento 2 Discount Rules for better control over your store’s promotions.

Don’t hesitate to reach out if you have any further questions or need assistance with your Magento development needs. We’re always here to help!

Happy coding!

Checkout Catalog Configuration Orders

Our network