Send out Gift Certificates to new users via the Coupon module

Posts: 96
Joined: 10/29/2007
Bug Finder

I wrote a nice little enhancement for those of you using the coupon module. This does two cool things.
1. When a new user registers, they get an email for a $5 gift certificate (coupon), the coupon is coded to be only used by them, and will expire in 14 days.
2. On the day the coupon expires, they will get an email reminding them to use the coupon. This email is only sent to them if the didn't use the coupon.

No changes to uc_coupons is required. Just put these into any hook_cron and hook_user on a module of yours.

This code sends out an email to all new registered users.

<?php
function SendNewUserCoupon() {
global
$user;
$coupon_code = strtolower(user_password(5));  // 5 char random string used for coupon
$expiration = mktime(0,0,0,date("n"),date("j")+14, date("Y"));  // set to expire in 14 days
$expiration_date = date("m/d/Y",mktime(0,0,0,date("n"),date("j")+14, date("Y"))); // expiration data to be displayed in email

// write record in coupon database for ubercart

$query = "insert into {uc_coupons} (name, code, value, type, status, valid_until, max_uses, users, roles, minimum_order) values ('$5 New User Gift Cert', '$coupon_code', 5,'price',1,$expiration,0,$user->uid,1,5)";
   
db_query($query);
// text of email
   
$email_body = "
    Welcome to the Ipod Fitness Center. As a special thank-you for joining our team, we are providing you with a $5 gift certificate that can be used toward any product on our website.  To use your gift certificate, simply visit the shopping section of our website
http://www.ipodfitnesscenter.com/ipodproducts and go shopping.  You'll see a Coupon field on the check-out screen.  Enter your gift certificate to save $5 on your ourder.

Your gift certificate number is: $coupon_code
This gift certificate is good through $expiration_date

    We are glad to have you as part of the Ipod Fitness Center team!
                "
;

 
drupal_mail("welcome-ifc-coupon",
            
$user->profile_fullname . '<' . $user->mail . '>',
            
'Your Ipod Fitness Gift Certificate',
            
$email_body,
            
"The Ipod Fitness Team <service@ipodfitnesscenter.com>");
}
?>

To use this code just put SendNewUserCoupon() into a hook_user() routine under the "insert" action. This way it is only called when they complete the registration process

This code is what makes the sale. Ladies especially hate to have coupons expire. This can be called from a hook_cron() and it will send out emails, if you want to send them out a day earlier, just change the query.

<?php
/*****************************************************************************/
function SendExpiringCoupon() {
global
$user;
// pull all the $5 coupons that have not been used on the day they expire
$query = "select uc_coupons.code, format(users,0) as uid from uc_coupons
left join uc_coupons_orders on uc_coupons_orders.code = uc_coupons.code
where  valid_until = unix_timestamp(curdate())  and isnull( uc_coupons_orders.code) and name = '$5 New User Gift Cert'
"
;
   
$result = db_query($query);
    while (
$row = db_fetch_object($result)) {
       
$account = user_load(array('uid' => $row->uid));
       
$email_body = "
    Two weeks ago, we emailed you an Ipod Fitness Center Gift Certificate. This is just a quick reminder to let you know that your Gift Certificate will Expire at midnight tonight.  Don't lose your $5 visit <a href="
http://www.ipodfitnesscenter.com/ipodproducts" title="http://www.ipodfitnesscenter.com/ipodproducts" rel="nofollow">http://www.ipodfitnesscenter.com/ipodproducts</a> and go shopping. 

Your gift certificate number is: $row->code
This gift certificate will expire
: TONIGHT

    We are glad to have you
as part of the Ipod Fitness Center team!
               
";
  drupal_mail("
expire-ifc-coupon",
             $account->profile_fullname . '<' . $account->mail . '>',
             'Ipod Gift Certificate Expires Tonight',
             $email_body,
             "
The Ipod Fitness Team <service@ipodfitnesscenter.com>");
    }
}
?>

Don't forget to put this in your query "name = '$5 New User Gift Cert' or whatever you name it, you only want to send out notices to the queries that have a value in the users column, and you may not want to send out notices on every expiring coupon.

I send my emails as plain text, but if you like them formated HTML, it's easy. Just put the HTML in your email body, add a $headers variable, and pass the headers to the drupal_mail function as follows.

<?php
$welcome_body
= "
                    <p>
                    <img src=\"http://www.ipodfc.com/img/ifclogo.jpg\"><br>               
                    </p>
                    <p>Thank you for joining IPODFITNESSCENTER.COM &ndash; our team is SO EXCITED and  will help you get the most out of your membership to our exclusive  community.&nbsp; You are now linked to the state-of-the-art fitness tracking  service and a very active fitness social network.&nbsp; We go beyond the gym, to make fitness FUN!</p>
    <p>We are a fitness center that you fit into your life so be sure to check out  and join in our monthly open team challenges. Invite your friend in Phoenix or your mom in Muncie to join you on these fun challenges  and keep fit and continue to build your relationships even when you are miles  apart. As a member, all of you will also enjoy current health news, wellness tips and member updates. &nbsp;&nbsp;</p>
    <p>To get an overview of the site, click on Fitness Flash from the Home Page.  Other  great site services include a FAQ&rsquo;s section and online &ldquo;Orientation&rdquo; telephone  training seminars.&nbsp; Dates and times are  listed on the site.<br />
    It&rsquo;s time to get fit, have fun, and be a part of the Ipod Fitness Center team!</p>
    <p><strong>Jim Fulford</strong><br />
      President<br />
      Ipod Fitness Center </p>
                "
;
$headers = array("Content-Type" => "text/html;"); 
   
drupal_mail("welcome-ifc-message",
            
$user->profile_fullname . '<' . $user->mail . '>',
            
'Welcome To IpodFitnessCenter.com',
            
$welcome_body,
            
"Ipod Fitness Center is Growing <jim@ipodfitnesscenter.com>",$headers);
?>

Enjoy
Jim Fulford
www.ipodfitnesscenter.com

Posts: 96
Joined: 10/29/2007
Bug Finder

One thing I noticed while implementing this today. The $user object is usually not set fully during user registration. So you may have to pass the $user values from hook_user insert to the function rather than using global $user to read them.

Thanks