Opencart 4 · 6 min read · Updated

Refer a Friend for OpenCart 4 and 3: Referral Links, Welcome Coupons and Rewards (Free Extension)

Give $10, get $10 in OpenCart: personal referral links, one-time welcome coupons and automatic store credit or reward points when the first order completes.

Opencart refer a friend

Your happiest customers already recommend you to friends. Without a referral program you can’t see it, you can’t thank them, and the friend has no extra reason to buy now instead of later. OpenCart has coupons, store credit and reward points built in, but nothing that ties them together into “give $10, get $10”.

Refer a Friend is a free extension that does exactly that. Every customer gets a personal link in My Account. A friend who signs up through it gets a one-time welcome coupon that’s already applied to the cart. When the friend’s first order is complete, the referrer gets store credit or reward points automatically. It comes in builds for OpenCart 4.1.0.4 and OpenCart 3.0.5.1, and we tested the full flow on both, from the shared link to the reward.

Download Refer a Friend

Refer a Friend page in an OpenCart 4 customer account with the offer text, a referral link with a Copy button, Email, WhatsApp and X share buttons, how it works steps and a list of referrals
The customer’s Refer a Friend page: the link, share buttons and the friends who signed up.

How the referral flow works

  1. The customer shares a link. In My Account there’s a new Invite friends and earn rewards link. It opens a page with their personal link, such as https://your-store.com/?ref=9ADCAB7D, a Copy button and Email, WhatsApp and X share buttons.
  2. The friend clicks it. The store remembers the code in a cookie for 30 days (you can change that) and shows a banner: “Jane invited you! Create an account to get 10% off your first order.”
  3. The friend creates an account. A one-time coupon is created just for them and applied to their cart right away, and a welcome banner shows the code.
  4. The friend’s first order is completed. When the order reaches a reward status (Complete by default), the referrer gets the reward as store credit or reward points, with a note in their account history.
OpenCart 4 storefront with a green banner at the top saying Jane invited you, create an account to get 10% off your first order
A friend who opens the link sees the invitation once.
OpenCart 4 cart totals showing a Refer a Friend coupon taking 20.20 dollars off a 202 dollar sub-total
After signing up, the friend’s coupon is already in the cart: 10% off 2 iPhones is -$20.20.

Install in OpenCart 4

  1. Go to Extensions > Installer, upload referafriend.ocmod.zip and click the green +.
  2. Go to Extensions > Extensions, choose Modules, and install Refer a Friend.
  3. Click the pencil, turn Status on, set the friend’s discount and the referrer’s reward, and save.
  4. Check that Extensions > Extensions > Order Total > Coupon is enabled, plus Store Credit or Reward Points for the reward you chose.

Install in OpenCart 3

  1. Go to Extensions > Installer and upload referafriend.ocmod.zip. There’s no OCMOD file, so no modification refresh.
  2. Go to Extensions > Extensions, choose Modules, and install Refer a Friend.
  3. Edit it, set Status to Enabled, fill in the discount and reward, and click Save.
  4. Make sure the Coupon order total, and Store Credit or Reward Points, are enabled.

Settings

Refer a Friend settings in OpenCart 4: status, link parameter ref, remember the link for 30 days, friend discount 10 percent, minimum order sub-total, coupon valid for 30 days, referrer reward 10 store credit, minimum first-order total and reward order statuses
Settings in OpenCart 4.1.0.4.
  • Link parameter: the name in the link, ref by default. Use 1 to 20 lowercase letters, numbers or underscores.
  • Remember the link for (days): how long the cookie lasts after a click. The default is 30.
  • What the friend gets: a percentage or a fixed amount off, an optional minimum sub-total, and how many days the coupon stays valid. Each coupon works once, only for that logged-in customer.
  • What the referrer gets: store credit in your default currency or a number of reward points, and an optional minimum total for the friend’s first order.
  • Reward when the order status is: usually just Complete, so canceled and refunded orders earn nothing. The extension preselects your store’s complete statuses. On a default OpenCart 3 store that includes Shipped, so untick it if you only want to pay after delivery.

Below the settings there’s a Referrals table: who referred whom, the friend’s coupon, the sign-up IP, the status and the rewarded order.

Referrals table in the OpenCart 4 admin listing Jane Doe as referrer, Maya Lopez as friend, coupon code, sign-up IP, status Rewarded and order 900022
The referrals list in the admin.

What we tested

On OpenCart 4.1.0.4, Jane shared her link and Maya Lopez signed up through it. Maya’s coupon was created and applied automatically, taking $20.20 off two iPhones. We moved Maya’s order to Processing: no reward, as expected. We moved it to Complete: Jane’s account got $10.00 store credit with the description “Refer a Friend: Maya placed their first order (#900022)”. Setting Complete a second time didn’t pay again.

On OpenCart 3.0.5.1 we ran the same steps with a new customer, Liam Chen. The coupon took $20.20 off two iPhones, Pending and Processing paid nothing, and Complete added $10.00 to the referrer’s store credit, once.

We also tried to cheat it. A customer who opens their own link is ignored, an unknown code is ignored, and a friend who signs up with the referrer’s own email address gets no coupon.

How it works

No core files are changed. On OpenCart 4 the extension adds a startup action that reads the ?ref= code on any page, and four events: one after a customer is added, one after an order history is added, one on the header for the banners, and one on the account page for the link. The startup action ignores unknown codes and self-referrals before setting the cookie:

$code = strtoupper(preg_replace('/[^A-Za-z0-9]/', '', $this->request->get[$param]));

$referrer = $this->db->query("SELECT c.`customer_id`, c.`firstname` FROM `" . DB_PREFIX . "referafriend_code` rc LEFT JOIN `" . DB_PREFIX . "customer` c ON (c.`customer_id` = rc.`customer_id`) WHERE rc.`code` = '" . $this->db->escape($code) . "' AND c.`status` = '1'")->row;

// Unknown codes and people clicking their own link are ignored.
if (!$referrer || ($this->customer->isLogged() && $this->customer->getId() == $referrer['customer_id'])) {
    return;
}

$days = max(1, (int)$this->config->get('module_referafriend_days'));

setcookie($this->cookie, $code, [
    'expires'  => time() + $days * 86400,
    'path'     => '/',
    'secure'   => !empty($this->request->server['HTTPS']),
    'httponly' => true,
    'samesite' => 'Lax'
]);

The reward runs after OpenCart saves an order status. Admin status changes go through the same catalog model, so they trigger it too. To make sure a referral can only pay once, even if two status changes arrive at the same moment, it first claims the referral with an update that only succeeds while the status is still pending:

$referral = $this->db->query("SELECT * FROM `" . DB_PREFIX . "referafriend` WHERE `friend_id` = '" . (int)$order['customer_id'] . "' AND `status` = 'pending'")->row;

if (!$referral) {
    return;
}

// Claim the referral first, so two status changes at once can't pay twice.
$this->db->query("UPDATE `" . DB_PREFIX . "referafriend` SET `status` = 'rewarded', `order_id` = '" . $order_id . "', `date_rewarded` = NOW() WHERE `referral_id` = '" . (int)$referral['referral_id'] . "' AND `status` = 'pending'");

if (!$this->db->countAffected()) {
    return;
}

What’s different in the OpenCart 3 build

Refer a Friend page on OpenCart 3.0.5.1 with the referral link, copy button, share buttons and a referral list showing Liam as Rewarded
The customer page on OpenCart 3.0.5.1, after Liam’s order was completed.
  • OpenCart 3 has no startup actions, so the link is captured in the catalog/view/common/header/after event, which runs on every storefront page.
  • The reward listens to catalog/model/checkout/order/addOrderHistory/after, OpenCart 3’s name for the same step.
  • Templates use Bootstrap 3 and Font Awesome 4, and the admin uses OpenCart 3’s normal save-and-reload form.
Refer a Friend settings form in the OpenCart 3 admin
The settings form on OpenCart 3.

Things to know

  • The coupon lives in the sign-up session. If the friend signs up, leaves, and comes back later on another device, the coupon isn’t in the new cart automatically. The welcome banner shows the code, so they can type it in the coupon box.
  • The friend must stay logged in. Coupons are created for logged-in customers only, so a friend can’t pass their coupon on to someone else.
  • Uninstalling removes the referral records, but coupons, credit and points that were already given stay in OpenCart.

Please let us know if you have any questions or suggestions, and subscribe to our YouTube channel for more OpenCart video tutorials. You can also find us on Twitter and Facebook.

Rupak Nepali
Written byRupak Nepali

Rupak Nepali is a web developer, manage Webocreation website and the author of six books:
Getting Started with OpenCart Module Development (Packt, 2013),
OpenCart Theme and Module Development (Packt, 2015),
OpenCart 4: Dev Guide for Themes & Extensions (2024),
OpenCart 4: User Manual (2025),
Cloudflare for the Enterprise Website (2026) and
The Technical Web Guide for Marketers (2026).
Rupak has built OpenCart stores and extensions since 2010, has published more than 600 tutorials on Webocreation, and shares free OpenCart extensions on GitHub.
Find Rupak on
GitHub | Amazon | LinkedIn | X | YouTube.

Leave a Reply

Your email address will not be published. Required fields are marked *