Magento-通过代码创建唯一的优惠券代码并将其邮寄给客户 [英] Magento - Create Unique Coupon Codes through code and mail it to the customer

查看:84
本文介绍了Magento-通过代码创建唯一的优惠券代码并将其邮寄给客户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有一种方法可以每次通过代码或某些脚本创建唯一的折扣券代码,然后根据需要将其邮寄给不同的客户.这是我在网上找到的脚本,

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();

}

此脚本创建了一个巨大的26个字母的唯一代码,效果差不多.我对该代码有些了解,但并不完全了解,因此每次都不知道如何创建6-7个字母的唯一代码并将其邮寄给客户.我也不确定应该如何将这些代码邮寄给我的客户.

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.

在编写提供的@Jitendra代码后,优惠券代码可以正常工作,并且可以正常创建.现在,如何在模块的IndexController.php函数中调用此文件?另外,我如何根据以下条件将此优惠券代码邮寄给每个不同的客户:

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*/

}

推荐答案

请检查一下,我也有相同的要求,您可以使用此代码并以自己的方式对其进行修改..

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

即时生成的Magento折扣coupan代码但不能正常工作

欢呼...

Jeet.

这篇关于Magento-通过代码创建唯一的优惠券代码并将其邮寄给客户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆