2012-01-18 60 views
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*/ 

} 

回答

0

認罪檢查這個我也可以使用此代碼並修改它自己的方式同樣的要求..

Magento discount coupan code created on fly but not working properly

歡呼聲......

截拳道。

+0

感謝您的回覆。我或多或少地做了同樣的事情,它爲我創建了優惠券代碼。我還沒有用它來檢查它是否有效。但首先,我需要找出一種方法,以便我每次在郵件中如何向客戶發送唯一的優惠券代碼。其次,我需要生成一個小代碼,而不是現在爲我創建的26個字母的代碼。你有什麼想法,我怎麼能實現這兩個? – ivn 2012-01-18 11:25:08

+0

請檢查代碼是否正確,它包含了所有的要求,你可以在函數中傳遞代碼的長度,參見我已經給出了8個。並且就電子郵件而言,您必須以您的方式修改代碼,只需在電子郵件中發送此代碼並且客戶可以使用它查看購物車頁面。 – Jitendra 2012-01-18 11:34:44

+0

您能告訴我您是用一個文件或現有文件的函數編寫的代碼嗎?我應該考慮哪兩個答案,哪一個是你或者其他人的答案。我剛剛使用了我使用上面的函數創建的代碼,它說'優惠券無效'。所以希望你的代碼在正確的地方和正確的文件,我不會'優惠券無效'的錯誤。 – ivn 2012-01-18 11:44:36

相關問題