Creating welcome coupons for your clients in PrestaShop

In this quick tip we will create welcome coupons for our clients, to reward the ones who decide to sign up for an account on our shop.

In order to create a discount code for clients who register new accounts on our store, we must edit the AuthController file. As always, the best practice is to create an override for it, so let’s add a new one inside override/controllers/front/, named AuthController.php
Open it up and add some generic override code inside php tags:

Class AuthController extends AuthControllerCore
{

}

The method we are interested in is sendConfirmationMail. We can get it straight out of the original controller, and I suggest you take it from yours since it might differ from mine (I am using PrestaShop 1.6.1.6):

    protected function sendConfirmationMail(Customer $customer)
    {
        if (!Configuration::get('PS_CUSTOMER_CREATION_EMAIL')) {
            return true;
        }

        return Mail::Send(
            $this->context->language->id,
            'account',
            Mail::l('Welcome!'),
            array(
                '{firstname}' => $customer->firstname,
                '{lastname}' => $customer->lastname,
                '{email}' => $customer->email,
                '{passwd}' => Tools::getValue('passwd'))
            $customer->email,
            $customer->firstname.' '.$customer->lastname
        );
    }

Copy and paste it inside our new override. At this point we can add our code to create the new cart rule, right before it returns the Mail::Send method:

        $cartRuleObj = new CartRule();
        $cartRuleObj->date_from = date('Y-m-d H:i:s');
        $cartRuleObj->date_to = '2046-12-12 00:00:00';
        $cartRuleObj->name[Configuration::get('PS_LANG_DEFAULT')] = 'Welcome coupon';
        $cartRuleObj->quantity = 1;
        $code = Tools::passwdGen();
        while (CartRule::cartRuleExists($code)) { // let's make sure there is no duplicate
            $code = Tools::passwdGen();
        }
        $cartRuleObj->code = $code;
        $cartRuleObj->quantity_per_user = 1;
        $cartRuleObj->reduction_percent = 20;
        $cartRuleObj->reduction_amount = 0;
        $cartRuleObj->free_shipping = 0;
        $cartRuleObj->active = 1;
        $cartRuleObj->minimum_amount = 0;
        $cartRuleObj->id_customer = $customer->id;
        $cartRuleObj->add();

For this example, I am creating a coupon with discount code that will give 20% off the price, that is going to be valid for a virtually indefinite period of time, with a single usage and restricted to this customer.
Make sure you add the following line:


$cartRuleObj->id_customer = $customer->id;

As it is what binds the rule to the current customer instance. Also notice I am generating a random code, and checking if there is any identical one in the database already using a while loop. While there is, we try creating a new one.

At this point, as soon as the customer accesses his account page, he will see the code in the My coupons section. However, as good practice we should really inform him in the account creation email as well. Let’s therefore amend the Mail:Send call as follows, by passing $code to the list of variables:

 return Mail::Send(
            $this->context->language->id,
            'account',
            Mail::l('Welcome!'),
            array(
                '{firstname}' => $customer->firstname,
                '{lastname}' => $customer->lastname,
                '{email}' => $customer->email,
                '{passwd}' => Tools::getValue('passwd')),
                '{coupon}' => $code,
            $customer->email,
            $customer->firstname.' '.$customer->lastname
        );

Great, we are done with php. What we need to do now is edit the corresponding account.html and account.txt emails to reflect the {coupon} variable.
Here is the final php code:

    protected function sendConfirmationMail(Customer $customer)
    {
        if (!Configuration::get('PS_CUSTOMER_CREATION_EMAIL')) {
            return true;
        }

        // nemo tut welcome coupon
        
        $cartRuleObj = new CartRule();
        $cartRuleObj->date_from = date('Y-m-d H:i:s');
        $cartRuleObj->date_to = '2046-12-12 00:00:00';
        $cartRuleObj->name[Configuration::get('PS_LANG_DEFAULT')] = 'Welcome coupon';
        $cartRuleObj->quantity = 1;
        $code = Tools::passwdGen();
        while (CartRule::cartRuleExists($code)) { // let's make sure there is no duplicate
            $code = Tools::passwdGen();
        }
        $cartRuleObj->code = $code;
        $cartRuleObj->quantity_per_user = 1;
        $cartRuleObj->reduction_percent = 20;
        $cartRuleObj->reduction_amount = 0;
        $cartRuleObj->free_shipping = 0;
        $cartRuleObj->active = 1;
        $cartRuleObj->minimum_amount = 0;
        $cartRuleObj->id_customer = $customer->id;
        $cartRuleObj->add();


        return Mail::Send(
            $this->context->language->id,
            'account',
            Mail::l('Welcome!'),
            array(
                '{firstname}' => $customer->firstname,
                '{lastname}' => $customer->lastname,
                '{email}' => $customer->email,
                '{passwd}' => Tools::getValue('passwd')),
                '{coupon}' => $code
            $customer->email,
            $customer->firstname.' '.$customer->lastname
        );
    }

Editing emails in PrestaShop

We have two ways to edit emails in PrestaShop: one is through the back office translations interface, the other is manual. Either will be fine, but I personally usually prefer to modify the html code directly.

Editing PrestaShop emails using the back-office interface

If you choose the first way, you can simply log in to the back office, and reach Localization > Translations. Choose
Email Templates translations from the first dropdown, then your template, and lastly the language, then hit modify.
In the next screen, toggle the Core emails section, and look for one labeled account.

prestashop_welcome_coupon_edit_email

What we want to do is edit the HTML template first, so click on Edit HTML version, and find a good spot where to add the new variable:

prestashop_welcome_coupon_email

Save, then do the same with the text version.

Editing PrestaShop emails’ HTMl directly

The second way is the one I prefer, as the built in editor can often be confusing, when not messy. Once more, we need to make sure we are editing the correct email templates, usually located in the current theme’s folder /mails/ followed by the language ISO.
The files we are interested in are account.html and account.txt. Here is the snippet I added to the html version”

<tr>
    <td class="space_footer" style="padding:0!important">&nbsp;</td>
</tr>
<tr>
    <td class="box" style="border:1px solid #D6D4D4;background-color:#f8f8f8;padding:7px 0">
        <table class="table" style="width:100%">
            <tr>
                <td width="10" style="padding:7px 0">&nbsp;</td>
                <td style="padding:7px 0">
                    <font size="2" face="Open-sans, sans-serif" color="#555454">
                        <p style="border-bottom:1px solid #D6D4D4;margin:3px 0 7px;text-transform:uppercase;font-weight:500;font-size:18px;padding-bottom:10px">20% off welcome code: {coupon}</p>
                        
                    </font>
                </td>
                <td width="10" style="padding:7px 0">&nbsp;</td>
            </tr>
        </table>
    </td>
</tr>

Which is basically a copy/paste of the one above, with a modified text. Once again, edit the txt version to add the variable there too.

At this point, make sure the modification is working by creating a new account and seeing if your customer gets the new email template, we are done!

You like the tuts and want to say "thank you"? Well, you can always feel free to donate:

  • Shruthi S M

    Nemo P S

    I am able to get email after registering but coupon code is not reflecting in the registered account.

  • Andrea

    Excellent article.
    To make the discount may not be combined (cumulable) ?

    • NemoPS

      I don’t think it’s simple, it probably needs an entry for each other cart rule to deny it. As far as I remember at least

      • Andrea

        An example ?

        Thanks

You like the tuts and want to say "thank you"? Well, you can always feel free to donate: