[英]Magento - Create Unique Coupon Codes through code and mail it to the customer
我想知道是否有一種方法可以每次通過代碼或某些腳本創建唯一的折扣優惠券代碼,然后根據需要將其郵寄給不同的客戶。 這是我在網上找到的腳本,
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*/
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.