WeFund Affiliate Tracking

WordPress Plugin Specification for affiliate referral tracking

Developer Integration Guide

Overview

Plugin Objective

This guide explains how to implement affiliate tracking on the WordPress site + WooCommerce, using the existing custom plugin that sends order data to WeFund API.

Key Objectives:
  • Capture affiliate referral codes from URL
  • Store them in a cookie for long duration (e.g., 30 days)
  • Attach referral code to WooCommerce orders as metadata (affiliate_code)
  • Ensure the plugin's API request to WeFund includes affiliate_code

Implementation Flow

1 Capture Referral Code from URL

When a user lands on a URL like:

https://we-fund.com/register?ref=WEF200751

The ref value should be extracted and stored in a cookie.

Cookie Specifications:
  • Name: wefund_affiliate_ref
  • Lifetime: 30 days
  • Scope: Entire domain (/)

2 Attach Referral Code to WooCommerce Orders

When an order is created, copy the cookie value into order metadata.

Metadata Specifications:
  • Key: affiliate_code
  • Value: e.g., WEF200751

3 Send Referral Code in Plugin API Call

Extend the existing payload to include the referral code when sending to the WeFund API.

Implementation Note:

The affiliate code should be included even if null (when no affiliate referral is present).

Code Examples

Capture Referral Code from URL
PHP Code Example
function wefund_capture_affiliate_ref() {
    if (isset($_GET['ref']) && !empty($_GET['ref'])) {
        $ref_code = sanitize_text_field($_GET['ref']);
        // Store cookie for 30 days
        setcookie('wefund_affiliate_ref', $ref_code, time() + (30*24*60*60), '/');
    }
}
add_action('init', 'wefund_capture_affiliate_ref');
Attach Referral Code to WooCommerce Orders
PHP Code Example
add_action('woocommerce_checkout_create_order', function($order, $data) {
    if (isset($_COOKIE['wefund_affiliate_ref'])) {
        $order->update_meta_data('affiliate_code', 
            sanitize_text_field($_COOKIE['wefund_affiliate_ref']));
    }
}, 10, 2);
Send Referral Code in API Call
PHP Code Example
// Inside your existing order processing function
$affiliate_code = $order->get_meta('affiliate_code');

$payload = [
    'order_id'        => $order->get_id(),
    'email'           => $order->get_billing_email(),
    'amount'          => $order->get_total(),
    'affiliate_code'  => $affiliate_code,  // Must be included
    // ... other fields already sent
];

$response = wp_remote_post('https://api.we-fund.com/orders/', [
    'method'    => 'POST',
    'headers'   => ['Content-Type' => 'application/json'],
    'body'      => wp_json_encode($payload),
    'timeout'   => 30,
]);

Payload Example

Example API Payload

When an order is placed, the plugin should send a payload similar to:

JSON Example
{
  "order_id": 12345,
  "email": "client@email.com",
  "amount": 299.00,
  "affiliate_code": "WEF200751"
}
The affiliate_code field should always be included in the payload, even if it's null (when no affiliate referral is present).

Security Notes

Important Security Considerations
Input Sanitization

Always use sanitize_text_field() for safety before saving/using codes.

No Overwriting

Do not overwrite affiliate_code if already attached to an order.

Consistent Data

Ensure the affiliate_code field is always sent, even if null.

Cookie Security

Consider adding appropriate cookie flags for security (httponly, secure).