2012-01-18 7 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文字のユニークなコードを作成して顧客に郵送する方法を知らないのです。これらのコードを顧客に郵送する方法についてもわかりません。

ご意見やご提案をいただければ幸いです。ありがとう。

EDIT:コード@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

歓声...

JEET。

+0

ご返信ありがとうございます。私は多かれ少なかれ同じことをやっているし、私のクーポンコードを作成します。私はまだそれが有効かどうかをチェックするためにそれを使用していない。しかし、まず私はどのように顧客にメールで一意のクーポンコードを送るべきかを知る方法を見つけ出す必要があります。第二に私はそれのために今作成する26文字のコードに比べて小さなコードを生成する必要がありますか?私はこれら2つをどのように達成することができますか? – ivn

+0

すべての要件が含まれているコードを正しく確認してください。関数のコードの長さを渡すことができます。私は8を与えています。そして、電子メールでこのコードを電子メールで送信したコードを変更する必要があります顧客はビューカートのページでそれを使用することができます。 – Jitendra

+0

ファイルまたは既存のファイルの関数で書かれたコードは教えてください。そして、私が考慮する必要がある2つの答えのうち、あなたまたは他の人のものを考慮する必要があります。私は上記の関数を使って作成したコードを使用しましたが、 'クーポンが無効です'とは言いませんでした。だからうまくいけば、適切な場所に適切なファイルをあなたのコードで、私は 'クーポン無効な'エラーが表示されません。 – ivn

関連する問題