The Challenge
Distributing unique one-time discount codes through Mautic presents certain challenges. You may need to send personalized discount codes for use in specific cases such as:
•Signing up for a newsletter and receiving a unique discount code
•Creating a single-use, time-limited discount code for abandoned cart users
We’ve developed a simple solution for these use cases that anyone with basic technical skills can implement.
Solution Overview
To distribute unique discount codes, we’ll set up a system using a bash script, a MySQL database, and a webhook server. This solution is designed for those with a basic understanding of Linux and MySQL.
Requirements
To implement this solution, you’ll need:
•Basic knowledge of Linux
•Basic knowledge of MySQL
•Access to your server (we’ve implemented this on Ubuntu)
•A working Mailertizer-powered AMS setup
•Access to WooCommerce and the ability to install a plugin
Setup Procedure
1.Create a New Database for Discount Codes
Start by creating a new database to store and manage your discount codes. Run the following commands from your terminal (where Mautic is installed):
mysql -u root -p
CREATE DATABASE DiscountSystem;
USE DiscountSystem;
CREATE TABLE DiscountCodes (
id INT AUTO_INCREMENT PRIMARY KEY,
coupon_code VARCHAR(255),
lead_id VARCHAR(20) NOT NULL,
date_assigned DATE NOT NULL
);
This creates a table with the following columns:
•ID: Auto-incremented for each new record
•Coupon Code: Holds the unique discount codes
•Lead_ID: Corresponds to the user ID from Mautic’s leads table
•Date Assigned: The date the discount code is assigned to a user
2.Generate Unique Discount Codes
You’ll need a bulk generator to create unique discount codes. If you’re using WooCommerce, we recommend the Coupon Generator for WooCommerce plugin.
Once you’ve generated the codes, export them to a CSV file, then transfer the file to your Mautic instance.
3.Import Discount Codes into the Database
You can import your unique discount codes from the CSV file into your database using a bash script:
for x in `cat unique_discount_codes.csv`; do mysql -u root DiscountSystem -e “insert into DiscountCodes set coupon_code = ‘$x'”; echo $x; done
This command loops through the CSV file and inserts each code into the DiscountCodes table. To check that everything was imported correctly, run:
mysql -u root DiscountSystem -p
SELECT * FROM DiscountCodes;
4. Install Webhook Service
To allow Mautic to interact with your discount codes database, you need a webhook service. We recommend using webhook.d, a simple tool that listens for webhooks and triggers actions based on them.
To install webhook.d, run:
sudo curl -s https://raw.githubusercontent.com/ncarlier/webhookd/master/install.sh | bash
After installation, configure basic authentication and ensure the service starts automatically on server reboot. You can set up security using htpasswd:
htpasswd -B -c .htpasswd api
Then, generate the base64 encoded version of username:password using this tool.
5.Prepare the Mautic Environment
For this example, we’ll focus on abandoned carts, but the approach applies to any type of campaign.
Assuming you already have abandoned cart data synced with Mautic, create two custom fields in Mautic:
•abandoned_cart (Boolean: Yes/No)
•discount_code (Text)
Create a Mailer template (e.g., “abandoned_cart_mailer-1”) to send the unique discount code:
You forgot something in your cart! Here’s a 10% discount to complete your purchase:
{contactfield=discount_code}
6.Set Up the Abandoned Cart Campaign
Now, create a new campaign. Use the Abandoned_Carts_Segment as the starting point.
•Add a Send Webhook action
•Add a Send Email action with a 5-minute delay, choosing the abandoned_cart_mailer-1 template
7.Create the Bash Script for Distribution
This script assigns a discount code to each abandoned cart user. Create a new script:
cd .local/bin
mkdir scripts
nano distribute_discount_codes.sh
Add the following script:
#!/bin/bash
# Get the first unassigned discount code
current_id=$(mysql -u root DiscountSystem -p -sN -e “SELECT id FROM DiscountCodes WHERE lead_id IS NULL LIMIT 1”)
echo $current_id
# Get today’s date
today_date=$(date ‘+%Y-%m-%d’)
echo $today_date
# Assign the discount code to the user
mysql -u root DiscountSystem -p -e “UPDATE DiscountCodes SET lead_id = ${id}, date_assigned = ‘$today_date’ WHERE id = $current_id”
# Get the assigned coupon code
woo_discount_code=$(mysql -u root DiscountSystem -p -sN -e “SELECT coupon_code FROM DiscountCodes WHERE lead_id = ${id} ORDER BY id DESC LIMIT 1”)
# Update the Mautic lead with the discount code
mysql -u root mautic -p -e “UPDATE leads SET discount_code = ‘$woo_discount_code’ WHERE id = ${id}”
Make the script executable:
chmod 755 distribute_discount_codes.sh
8.Configure Webhook in Mautic
In the Send Webhook action of your Mautic campaign, set the URL to:
http://localhost:8080/distribute_discount_codes
Set the method to POST and add the Authorization header with your encoded username:password.
Final Thoughts
With this setup, Mautic will trigger the webhook when a user enters the Abandoned Cart segment. The distribute_discount_codes.sh script assigns a unique discount code from the database to the user and updates the discount_code field in Mautic. This allows for personalized, one-time use discount codes to be sent to abandoned cart users, or any other campaign you choose.
If you have any questions or encounter issues with the setup, feel free to ask for support!