Magento Set discount code for specific product on fly

Magento Set discount code for specific product on fly

September 24, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento Set discount code for specific product on fly

Magento: Applying Discount Codes to Specific Products on the Fly

Hi Jitendra,

Thank you for reaching out again! I’m glad you found a solution that worked for applying a discount code to specific products in your previous question. In this response, I’ll provide you with a step-by-step guide on how to separate the discount and apply it specifically to the product mentioned when entering the discount code.

Understanding the Current Issue

In your current setup, the discount is applied to the grand total instead of being separated for the specific product. This happens because the rule conditions are not properly configured to match only the target product using its SKU (Stock Keeping Unit) or other unique identifiers.

Step-by-Step Solution

To achieve this, we need to modify the existing code by adding proper condition and action serializations. Here’s a line-by-line explanation of the updated code:

$conditions = array();
$conditions[1] = array(
    'type' => 'salesrule/rule_condition_combine',
    'aggregator' => 'all',
    'value' => 1,
    'new_child' => ''
);
$conditions['1--1'] = array
(
    'type' => 'salesrule/rule_condition_product_found',//-> means 'if all of the following are true' - same rules as above for 'aggregator' and 'value'
    //other values for type: 'salesrule/rule_condition_product_subselect' 'salesrule/rule_condition_combine'
    'value' => 1,
    'aggregator' => 'all',
    'new_child' => '',
);

$conditions['1--1--1'] = array
(
    'type' => 'salesrule/rule_condition_product',
    'attribute' => 'sku',
    'operator' => '==',
    'value' => $sku,
);

$model->setData('conditions', $conditions);
$model->loadPost($model->getData());

Let’s break down the key parts:

  1. Create an array for conditions with a unique index 1.
  2. Set the type to 'salesrule/rule_condition_combine' which means all following conditions must be true.
  3. The 'salesrule/rule_condition_product_found' condition checks if the product is found in the cart (value 1).
  4. The inner array '1--1--1' represents a single rule condition for the product itself.
  5. Set the type to 'salesrule/rule_condition_product', which targets a specific product.
  6. Use the 'sku' attribute and set the operator to == to match exactly with the provided SKU value.

After setting these conditions, use $model->loadPost($model->getData()) to update the model’s data with the new condition serialization.

Configuration for Actions

In addition to the correct conditions, ensure that your action serialization is also configured appropriately. You can find this in a similar array structure where you define the type as 'salesrule/rule_condition_product', set the attribute to price, and operator to fixed. This will apply the discount amount directly to the product’s price.

Conclusion

By carefully configuring the condition and action serializations, you can successfully separate the discount for a specific product when entering the generated coupon code. This approach targets the product using its SKU or other unique identifiers, ensuring that the discount is applied exclusively to the intended item.

Remember to save the model ($model->save();) after updating the data and conditions.

I recommend testing this solution in a development environment first before implementing it on your live Magento site. If you encounter any issues or need further assistance, please don’t hesitate to ask!

For more detailed tutorials and insights on Magento customizations, check out our blog at Laravel Company. I’m always happy to help with questions like these!

Catalog Configuration Pricing Errors

Our network