Referral Program
The LoyaltyBay Referral Program turns your customers into brand advocates by rewarding them when they introduce new customers to your store.
How It Works
Section titled “How It Works”- Share: Logged-in customers copy their unique referral URL from the My Account > Loyalty Points dashboard.
- Visit: A new visitor clicks the referral link and browses your site. The system drops a tracking cookie.
- Register: The new visitor registers an account on your store.
- Purchase: The new customer completes their first order.
- Reward:
- The Referrer receives bonus points (e.g. 1,000 points).
Technical Architecture & Cookie Capture
Section titled “Technical Architecture & Cookie Capture”Database Meta Keys
Section titled “Database Meta Keys”The referral system tracks associations using default WordPress user metadata:
loyaltybay_referral_code— Unique alphanumeric identifier code assigned to each customer.loyaltybay_referred_by— Stores the User ID of the referrer inside the referee’s user metadata.loyaltybay_referral_rewarded— A boolean check flag stored in the referee’s metadata preventing multiple referral reward triggers.
Cookie Tracking Flow
Section titled “Cookie Tracking Flow”When a visitor arrives at the site:
- Hook: The plugin hooks into
initduringtemplate_redirectexecution. - Check: It checks if the URL contains the query parameter
?ref=CODE. - Validate: It queries the database to find the User ID matching the referral code.
- Cookie Drop: If valid, it drops a browser cookie named
loyaltybay_refstoring the Referrer ID with a 30-day lifespan (configurable viareferrals_cookieDuration). - User Link: When the visitor registers a user account, the plugin checks for the
loyaltybay_refcookie. If found, it writes the Referrer ID to the new user’s meta underloyaltybay_referred_byand deletes the cookie.
Referral Reward Validation
Section titled “Referral Reward Validation”Rewards are finalized when the referee places their first order:
1. Order Completion Hook
Section titled “1. Order Completion Hook”The processing logic hooks into woocommerce_order_status_completed at priority 30 (after base purchase points are already awarded).
2. Validation Checks
Section titled “2. Validation Checks”Before crediting points, the referral processor (Referrals\ReferralProcessor) runs the following checks:
- Referred Check: Verifies if the buyer has the
loyaltybay_referred_bymeta key. - Double Trigger Check: Confirms the buyer does not have meta
loyaltybay_referral_rewardedset totrue. - Self-Referral Prevention: Verifies that the Referrer ID does not equal the Buyer ID (e.g., a customer trying to use their own referral link).
- First Order Verification: Runs a WooCommerce query to verify this is the customer’s first completed purchase:
If$order_count = count( wc_get_orders( ['customer' => $buyer_id,'status' => [ 'completed', 'processing' ],'limit' => 2,] ) );
$order_countis greater than1, the referral reward is rejected.
3. Credit Transaction
Section titled “3. Credit Transaction”If all validations pass:
- Credits the referrer with points (reference type:
referral, reference ID: Order ID). - Updates the referee’s metadata:
loyaltybay_referral_rewarded = true. - Fires a custom WordPress action hook:
loyaltybay_referral_processed.
Key Rules & Policies
Section titled “Key Rules & Policies”One-Time Earning Limit
Section titled “One-Time Earning Limit”Referral rewards are strictly one-time incentives. Points are only awarded to the referrer on the first completed order of the referee. Subsequent orders placed by the referee do not award additional points to the referrer.
Refund Isolation Policy
Section titled “Refund Isolation Policy”To protect referrer relationships and maintain customer trust, the system enforces a strict isolation policy:
[!NOTE] If a referred customer (referee) requests a refund on their first order, the referee’s earned points are clawed back, but the referrer’s referral bonus points are NOT deducted. This ensures referrers are not penalized for the return behavior of their referred friends.