Magento - Create Unique Coupon Codes through code

2019-09-03 20:56发布

问题:

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:

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.