Programmatically create auto generated coupon codes in Magento?

Programmatically create auto generated coupon codes in Magento?

August 17, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Programmatically create auto generated coupon codes in Magento?

Automatically Generating Coupon Codes in Magento

As an experienced Magento developer, I frequently receive requests from clients who want to programmatically generate coupon codes for their promotional campaigns. While the existing code snippets provided by the OP create individual shopping cart price rule coupons, they do not address the automatic generation of unique coupon codes.

To achieve this functionality, we need to use Magento’s built-in coupon generation feature, which is available through the “Auto Generate Coupon” option in the admin panel. Let me walk you through the process of programmatically calling this feature to create multiple unique coupons for each price rule.

Understanding the Auto Generation Feature

The setUseAutoGeneration method allows us to specify whether the coupon should be auto-generated or not. When set to 1, Magento automatically generates a unique code based on the configured pattern. The setCouponCode method is then ignored.

To enable automatic generation of coupons, you need to ensure that:

  1. You have enabled the “Auto Generation” option in your price rule settings.
  2. You have specified the number of coupons to generate using the setUsesPerCoupon method.
  3. The coupon type (setCouponType) is set to 2 (fixed amount).

Programmatically Generating Multiple Coupons

To create multiple unique coupons programmatically, you can leverage Magento’s price rule model and its auto-generation capabilities. Here’s an example of how to achieve this:

$couponModel = Mage::getSingleton('salesrule/coupon_model');

// Set the base parameters for the coupon
$baseParams = array(
    'name' => 'Auto Generated Coupon',
    'description' => 'This is a description',
    'from_date' => date('Y-m-d'),
    'to_date' => null,
    'is_active' => 1,
    'uses_per_customer' => 100,
    'coupon_type' => 2, // fixed amount
    'uses_per_coupon' => 1000, // number of coupons to generate
    'customer_group_ids' => array(1), // customer group ids
    'is_rss' => 0,
);

// Initialize the coupon model with base parameters
$couponModel->setData($baseParams);

// Generate the coupons
try {
    $couponModel->save();
} catch (Exception $e) {
    Mage::logException($e);
}

// Now, you can access the generated coupon codes using the following methods:
// - $couponId = $couponModel->getId(); // get the first coupon id
// - $couponCodes = $couponModel->getCouponCollection(); // fetch all the generated coupons

Mage::log('Auto generation of coupons completed successfully.');

This code snippet demonstrates how to create a new price rule with automatic coupon generation enabled. It initializes the salesrule/coupon_model and sets the required parameters, including the number of coupons to generate (uses_per_coupon).

When you call the $couponModel->save() method, Magento automatically generates the specified number of unique coupon codes based on the configured pattern.

Configuring the Coupon Generation Pattern

To customize the auto-generation pattern for your coupon codes, you need to access the configuration file located at app/code/core/Mage/SalesRule/etc/config.xml. Look for the <auto_generate_coupons> node under the <salesrule /> section.

You can modify the attributes and values within this node to define the pattern for your auto-generated coupons. The default configuration is as follows:

<auto_generate_coupons>
    <pattern>%04d</pattern>
    <digits_count>4</digits_count>
    <letters_count>1</letters_count>
    <prefix>COUPON-</prefix>
    <suffix>-FIXED</suffix>
</auto_generate_coupons>

In this example, the pattern is %04d, which generates a 4-digit numeric coupon code. You can change the pattern to suit your requirements.

The <digits_count> and <letters_count> attributes define the number of digits and letters in the generated code respectively. The <prefix> and <suffix> attributes allow you to add custom prefixes and suffixes to the auto-generated codes.

Conclusion

Automatically generating coupon codes is a powerful feature that allows merchants to create promotional campaigns with minimal effort. By leveraging Magento’s built-in auto-generation capabilities, we can programmatically create multiple unique coupons for each price rule without manually entering them in the admin panel.

In this blog post, I have demonstrated how to use Magento’s salesrule/coupon_model to generate auto-generated coupon codes with a specified number of uses per coupon. Additionally, I explained how to configure the auto-generation pattern within the config.xml file to customize the format of your coupon codes.

By following these best practices and utilizing the provided code snippets, you can seamlessly integrate automated coupon generation into your Magento projects and enhance the promotional capabilities for your clients.

Remember to test the functionality thoroughly in a development environment before deploying it to a live production site. Always ensure that your code is properly secured and adheres to Magento’s coding standards.

If you have any further questions or need assistance with implementing this feature, feel free to reach out to our team at Laravel Company. We specialize in providing expert Magento development services to help you drive success for your business.

Checkout Admin Configuration Pricing

Our network