Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7072749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:50:48+00:00 2026-05-28T05:50:48+00:00

I was wondering if there is a way through which one can create unique

  • 0

I was wondering if there is a way through which one can create unique Discount Coupon codes every time through code or some script and mail it to different customers as per needed. Here’s the script that I found over the net,

public function generateRuleAction()
{
    $rndId = crypt(uniqid(rand(),1));
    $rndId = strip_tags(stripslashes($rndId));
    $rndId = str_replace(array(".", "$"),"",$rndId);
    $rndId = strrev(str_replace("/","",$rndId));
    if (!is_null($rndId))
    {
        strtoupper(substr($rndId, 0, 5));
    }

    $groups = array();
    foreach ($customerGroups as $group)
    {
        $groups[] = $group->getId();
    }

    $websites = Mage::getModel('core/website')->getCollection();
    $websiteIds = array();
    foreach ($websites as $website)
    {
        $websiteIds[] = $website->getId();
    }

     $uniqueId = strtoupper($rndId);
     $rule = Mage::getModel('salesrule/rule');
     $rule->setName($uniqueId);
     $rule->setDescription('Generated for Test Purposes');
     $rule->setFromDate(date('Y-m-d'));//starting today
     //$rule->setToDate('2011-01-01');//if an expiration date's needed
     $rule->setCouponCode($uniqueId);
     $rule->setUsesPerCoupon(1);//number of allowed uses for this coupon
     $rule->setUsesPerCustomer(1);//number of allowed uses for this coupon for each customer
    $customerGroups = Mage::getModel('customer/group')->getCollection();

    $rule->setCustomerGroupIds($groups); 
    $rule->setIsActive(1);
    $rule->setStopRulesProcessing(0);//set to 1 if you want all other rules after this to not be processed
    $rule->setIsRss(0);//set to 1 if you want this rule to be public in rss
    $rule->setIsAdvanced(1);
    $rule->setProductIds('');   
    $rule->setSortOrder(0);// order in which the rules will be applied
    $rule->setSimpleAction('by_percent');

    $rule->setDiscountAmount('20');//the discount amount/percent. 
    //if SimpleAction is by_percent this value must be <= 100
    $rule->setDiscountQty(0);//Maximum Qty Discount is Applied to
    $rule->setDiscountStep(0);//used for buy_x_get_y; This is X
    $rule->setSimpleFreeShipping(0);//set to 1 for Free shipping
    $rule->setApplyToShipping(1);//set to 0 if you don't want the rule to be applied to shipping

    $rule->setWebsiteIds($websiteIds); 

    $conditions = array();
    $conditions[1] = array(
    'type' => 'salesrule/rule_condition_combine',
    'aggregator' => 'all',
    'value' => 1,
    'new_child' => ''
    );

    $conditions['1--1'] = Array
    (
    'type' => 'salesrule/rule_condition_address',
    'attribute' => 'base_subtotal',
    'operator' => '>=',
    'value' => 200
    );


    $labels = array();
    $labels[0] = 'Default store label';//default store label
    $labels[1] = 'Label for store with id 1';
    //....
    $labels[n] = 'Label for store with id n';
    //add one line for each store view you have. The key is the store view ID
    $rule->setData('conditions',$conditions);
    $rule->loadPost($rule->getData());
    $rule->setCouponType(2);
    $rule->setStoreLabels($labels);
    $rule->save();

}

This script creates a huge 26-letter unique code just about fine. I understand this code some what but not completely and hence do not know how to create a small 6-7 letter unique code each time and mail it to the customer. I am also not sure as to how I should go about mailing these codes to my customers.

Any input or suggestions would be well appreciated. Thanks.

EDIT :After writing the code @Jitendra provided, the coupon code works fine and gets created fine. Now how do I call this file in my function which is my module’s IndexController.php? Also how do I mail this coupon code to each different customer based on the following condition:

$sample_model2 = Mage::getModel('sample/sample')->getCollection();
$sample_model2->addFieldToFilter('order_email_id', $customerEmail);
foreach($sample_model2 as $final_model1)
{   
echo '<br/>Email: ' . $final_model1['order_id'] . '<br/>';  
/*NEED SOME FUNCTION TO BE CALLED HERE TO CREATE UNIQUE COUPON CODE FOR EACH EMAIL ID AND MAIL THEM TO THE CUSTOMER*/

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T05:50:49+00:00Added an answer on May 28, 2026 at 5:50 am

    pleas check this i have also the same requirement you can use this code and modify it your own way..

    Magento discount coupan code created on fly but not working properly

    cheers…

    Jeet.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm very new to Oracle and was wondering if there's a way either through
I am wondering is there any way we can test the font size/color of
I'm using Delphi 2010 and I'm wondering if there's a way to trace through
I was wondering if there was a way to completely lock my code while
I am wondering is there a way to intercept/monitor http traffic in android. One
I was wondering if there is way to open another page using a Modal
I was wondering is there a way in PHP that you could tell where
I am wondering is there a way to render a partial view in the
I think I understand unit testing. But I was wondering: is there a way
Wondering if there is any way to get the lambda expressions that result from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.