Send Promo Code to New Registered Customer in Magento

Hello Guys,

Recently I have performed a task for sending a promo code to new registered customers only, So that they can use/redeem this code in their next purchase.

Follow Below Steps to achieve this, I am assuming you are aware with the custom module development in magento

Step 1: Add below code into your etc/config.xml under global node

 <models> <!-- Defines your Model -->
            <your_module>
                <class>Your_Module_Model</class>
            </your_module>
        </models>
<template>
            <email>
              <notify_new_customer1>
                    <label>Template to send promo code for new registered customer</label>
                    <file>notify_new_customer.html</file>
                    <type>html</type>
                </notify_new_customer1>
            </email>
        </template>

Step 2:  After global tag, add below code to handle Obser/Event : because in my case I am sending email user Observer. Add below code into your etc/config.xml

<frontend>
        <events>
            <customer_save_after> <!-- or check with customer_register_success -->
                <observers>
                    <your_module>
                        <type>model</type>
                        <class>your_module/observer</class>
                        <method>customerSaveAfter</method>
                    </your_module>
                </observers>
            </customer_save_after>
        </events>
    </frontend>

 Step 3: Add HTML file into your app/locale/en_US/template/email folder

<h1>Congratulations, You have got a promo code with this email which you can redeem on your next purchase :</h1> <br />
Name: {{var username}}<br />
Email: {{var customer_email}}<br />
Promo Code: {{var promo_code}}<br />
...<br />



All variables written here under parenthesis would get set in step 4 ..see the function below.

Step 4: Add this function into your observer file under Model/Observer.php

  public function customerSaveAfter(Varien_Event_Observer $o)
    {
        //Array of customer data
        $customerData = $o->getCustomer()->getData();


           if (!$o->getCustomer()->getOrigData()) {
            //customer is new, otherwise it's an edit
          
            $modelcollection = Mage::getModel('salesrule/rule')->getCollection();
            $newCollection =array();
            $newCollection = $modelcollection->getData();

            $ruleName = $newCollection[0]['name'];
            $rule_id=   $newCollection[0]['rule_id'];
            $promocode = $newCollection[0]['code'];
        

            if($rule_id==1 && $ruleName=="RegisteredUserPromocode"){ // if in case not required remove this condition ...

                $emailTemplate  = Mage::getModel('core/email_template')
                    ->loadDefault('notify_new_customer1');

                $emailTemplate
                    ->setSenderName('prasad developer')
                    ->setSenderEmail('prasad@krishinc.com')
                    ->setTemplateSubject('Promo Code for New customer registered');
                // $data = $observer->getCustomer()->getData(); */
                $emailTemplateVariables = array();

                $emailTemplateVariables['username']= $customerData['firstname'].' '.$customerData['lastname'];
                $emailTemplateVariables['customer_email']   = $customerData['email'];
                $emailTemplateVariables['promo_code'] = $promocode;

                $result = $emailTemplate->send($customerData['email'],$customerData['firstname'].' '.$customerData['lastname'], $emailTemplateVariables);
            }
        }
    }




Step 5: Create a promo code for X percent discount from admin under Promotion/ Shopping Cart Price Rule

RULE INFORMATION
Rule Name:  RegisteredUserPromocode
Description: RegisteredUserPromocode(Applicable Only : First Time)
Status: Active
Customer Group: General
Coupon: Specific Coupon
Coupon Code: XXX1234
Uses per Coupon: 1
Uses per Coupon: 9999 (assuming that this much numbers of customers are not present so sending the same code all new customers one by one)

For Action tab see screenshot:
Screen shot


Note: 

=>If you would like to check 'parsed' content of email (with set variables value), you can do that by below code just before sending an Email               

$processedTemplate = $emailTemplate->getProcessedTemplate($emailTemplateVariables);
echo  $processedTemplate; // ;)


Hope this post will be useful! Cheers..

Comments

  1. Hi Dear,

    I am very new to magento, Please brief me about how can i
    Send Promo Code to New Registered Customer in Magento ,,,


    Please...

    ReplyDelete
  2. Please Provide me step by step - Send Promo Code to New Registered Customer in Magento.

    Please...
    Reply

    ReplyDelete

Post a Comment

Popular posts from this blog

Add/Remove Product from Wishlist using Ajax in Magento

Terraform Basics - To get Started