How To Automatically Send Birthday Emails and Personal Coupons With OpenCart

This is probably going to be the heftiest blog post I’ve ever written but I have developed a way for Opencart to recognise when it is a user’s birthday and send them a personalised email with a discount coupon that they can redeem within your store for a limited time. I will be going through the steps so you don’t need to be particularly technical minded as everything is provided but it does help if you are familiar with the OpenCart system.

Adding User Birthday Into OpenCart Database

OpenCart doesn’t come with a field for User Birthdays so we are going to have to add the entries into the database ourselves. A pretty painfree process, just log into your PHP MyAdmin (or equivalent) and locate the “opn_customer” table within the database. You then need to add two fields named:

  • birth_date
  • bday_email

Both of the fields are VARCHAR type with 60 character limits. We will be looking at what exactly these do later on but basically one gives the date of birth of the customer and the other stops us sending them a thousand emails on their actual birthday!

Editing The OpenCart Core To Use The User Birthday

We need to change a few files throughout the OpenCart system before we can start using the date of birth in the software, it’s is a fairly simple copy and paste job but make sure you follow every step or you will have trouble later!

Step 1
  • catalogviewthemedefaulttemplateaccountcreate.tpl

You will need to paste the following code into this file to harvest the customers date of birth when they first register with your store. This will be a required field and users can’t register without giving you this info:


Make sure to fit this block within the table that is already there otherwise it will render wrong, just insert it in between two existing rows on the Registration page.

Step 2
  • cataloglanguageenglishaccountcreate.php

You will need to add a line of text in this file to define the text which displays on the Registration page when a customer is signing up. Just add the following somewhere within the “ENTRY” block of variables:

$_['entry_birth_date']      = 'Birthday:';
Step 3
  • catalogcontrolleraccountcreate.php

This next block of code has been split into three sections, simply add the lines into the blocks in the file which look similar as below:

//Add this to the list of similar looking lines, doesn't matter where
$this->data['entry_birth_date'] = $this->language->get('entry_birth_date');

//Add this to the error lists, look for the lines $this->error['SOME TEXT']
if (isset($this->error['birth_date'])) {
            $this->data['error_birth_date'] = $this->error['birth_date'];
        } else {
            $this->data['error_birth_date'] = '';

//Add this to the POST variables, look for the lines $this->request->post['SOME TEXT']
if (isset($this->request->post['birth_date'])) {
            $this->data['birth_date'] = $this->request->post['birth_date'];
        } else {
            $this->data['birth_date'] = '';
Step 4


  • catalogmodelaccountcustomer.php

Now we need to edit a function located in the file above which updates the customer details to include the date of birth. I would advise against this as it could lead to customers being able to take advantage of the discounts but we will have a security block to protect you as well. Find the line which has “public function editCustomer($data) {” and add the following update to the MySQL Update Query just underneath it!

birth_date = '" . $this->db->escape($data['birth_date']) . "'

Personally, I don’t see the point of allowing a customer to change their date of birth as it generally stays the same, even after they have registered! So, feel free to skip this step out if you agree with me.

Building The OpenCart Birthday Email & Coupon Code

Now we have set the stage including the customer birthday within our database and adjusting the settings to allow OpenCart to use it. Now we need to build up the actual PHP function, which needs to have several steps:

  1. It will check if it is the Customer’s birthday
  2. It will check if they have already had a birthday email this year
  3. It will generate a birthday email
  4. It will automatically generate a coupon giving the user a birthday discount!

So, lets get started!

The first problem was how to make the function generate automatically but without the user themselves having to log on to the system. In the end I decided to put the PHP function in the header controller file so that it was tripped whenever anyone visited the site, regardless of whether they are logged in or not. So open up this file:

  • catalog/controller/common/header.php

I put the function about a third of the way down the page, look for the “// Calculate Totals” line and insert this function in a space just above it. I’ll walk you through what the function does step by step but you can always just copy and paste this information into a web file and follow the comments shown!

// Load the model to get access to the customer information

//Set Dates which you'll need later
$todayDate = date('dmY');
$thisYear = date('Y');

$startDate = date("Y-m-d");
$endDate = date('Y-m-d', strtotime('+ 1 week',strtotime($startDate)));

//Find the customers whose birthday is today
$findCustomers = mysql_query("SELECT * FROM opn_customer WHERE birth_date = '" . $todayDate . "'");
while($row = mysql_fetch_array($findCustomers)){
   $birthDate = $row['birth_date'];
   $birthEmail = $row['bday_email'];

   //This checks the birthday is today and they have not had an email already
   if($birthDate = $todayDate && $birthEmail != $thisYear){

   //This creates a unique coupon code
   $discountCode = '' . $row['customer_id'] . '' . $row['lastname'] . 'BDAY' . $thisYear . '';

   //This is setting the customer which is sent out, it is HTML and you can replace it with whatever you like   
   $to = $row['email'];
   $from = '[email protected]';
   $subject = 'Happy Birthday!';
   $theEmail = '

Many Happy Returns From Our Store

Happy Birthday ' . $row['firstname'] . '!

We are pleased to offer you 5% off you next order as a birthday prezzie from us! Just enter "' . $discountCode . '" when you next place an order to get your money off!

We hope you have a great day and look forward to seeing you again soon!


Jack Davis

'; $headers = "From: $fromrn"; $headers .= "Content-type: text/htmlrn"; mail($to, $subject, $theEmail, $headers); //Update the Database to set the bday_email to this year, stopping them getting another one when the script is run again $this->db->query("UPDATE " . DB_PREFIX . "customer SET bday_email = '" . $thisYear . "' WHERE customer_id = '" . $row['customer_id'] . "'"); //This creates the actual coupon, by default I have set it to 5% discount, no free shipping, no min total to spend, ending after one week $this->db->query("INSERT INTO " . DB_PREFIX . "coupon (name, code, type, discount, logged, shipping, total, date_start, date_end, uses_total, uses_customer, status, date_added) VALUES ('" . $row['firstname'] . " " . $row['lastname'] . " Birthday Discount', '$discountCode', 'P', '5.0000', '1', '0', '0', '" . $startDate . "', '" . $endDate . "', '1', '1', '1', '" . $startDate . "')"); } else { print ""; } }

This is quite a hefty function but as far as I can see it doesn’t slow down the OpenCart page loading speed noticeably. Keep in mind if you want to create a different coupon then you will have to edit the MySQL Insert Query above. You can check out what it needs to be from the OpenCart website or drop me a comment below and I’ll see if I can help you out!

Leave a Reply