0
我想知道是否有办法通过代码或某些脚本每次创建唯一的折扣优惠券代码并根据需要将其邮寄给不同的客户。这是我在网上找到的脚本,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*/
}
感谢您的回复。我或多或少地做了同样的事情,它为我创建了优惠券代码。我还没有用它来检查它是否有效。但首先,我需要找出一种方法,以便我每次在邮件中如何向客户发送唯一的优惠券代码。其次,我需要生成一个小代码,而不是现在为我创建的26个字母的代码。你有什么想法,我怎么能实现这两个? – ivn 2012-01-18 11:25:08
请检查代码是否正确,它包含了所有的要求,你可以在函数中传递代码的长度,参见我已经给出了8个。并且就电子邮件而言,您必须以您的方式修改代码,只需在电子邮件中发送此代码并且客户可以使用它查看购物车页面。 – Jitendra 2012-01-18 11:34:44
您能告诉我您是用一个文件或现有文件的函数编写的代码吗?我应该考虑哪两个答案,哪一个是你或者其他人的答案。我刚刚使用了我使用上面的函数创建的代码,它说'优惠券无效'。所以希望你的代码在正确的地方和正确的文件,我不会'优惠券无效'的错误。 – ivn 2012-01-18 11:44:36