Magento - 通过代码创建独特的优惠券代码并将其邮寄给客户

时间:2012-01-18 10:10:17

标签: php zend-framework magento

我想知道是否有办法可以通过代码或某些脚本每次创建唯一的优惠券代码,并根据需要将其邮寄给不同的客户。这是我在网上找到的脚本,

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字母唯一代码并将其邮寄给客户。我也不确定如何将这些代码邮寄给我的客户。

任何意见或建议都将受到赞赏。感谢。

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

$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 个答案:

答案 0 :(得分:0)

请检查这个我也有相同的要求你可以使用这个代码并按照自己的方式修改..

Magento discount coupan code created on fly but not working properly

...欢呼声

截拳道。