feat: Affiliate program enrichment (Link Builder, Curated Collections, Smart Links)

This commit is contained in:
Dwindi Ramadhana
2026-06-03 14:04:17 +07:00
parent fd8eb38512
commit f8c733832e
22 changed files with 1348 additions and 10 deletions

View File

@@ -17,6 +17,7 @@ class AffiliateManager
private static $affiliates_table = 'woonoow_affiliates';
private static $referrals_table = 'woonoow_referrals';
private static $payouts_table = 'woonoow_affiliate_payouts';
private static $collections_table = 'woonoow_affiliate_collections';
/**
* Initialize
@@ -37,6 +38,7 @@ class AffiliateManager
$affiliates_table = $wpdb->prefix . self::$affiliates_table;
$referrals_table = $wpdb->prefix . self::$referrals_table;
$payouts_table = $wpdb->prefix . self::$payouts_table;
$collections_table = $wpdb->prefix . self::$collections_table;
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
@@ -135,6 +137,23 @@ class AffiliateManager
}
}
// Collections Table
$sql_collections = "CREATE TABLE $collections_table (
id bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,
affiliate_id bigint(20) UNSIGNED NOT NULL,
title varchar(255) NOT NULL,
slug varchar(255) NOT NULL,
description text DEFAULT NULL,
product_ids longtext DEFAULT NULL,
created_at datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (id),
UNIQUE KEY affiliate_slug (affiliate_id, slug),
KEY affiliate_id (affiliate_id)
) $charset_collate;";
dbDelta($sql_collections);
// Payouts Table
$sql_payouts = "CREATE TABLE $payouts_table (
id bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,

View File

@@ -108,6 +108,12 @@ class AffiliateModule
}
}
}
// Create collections table if it doesn't exist
$collections_table = $wpdb->prefix . 'woonoow_affiliate_collections';
if ($wpdb->get_var("SHOW TABLES LIKE '$collections_table'") !== $collections_table) {
AffiliateManager::create_tables();
}
}
/**

View File

@@ -58,6 +58,12 @@ class AffiliateSettings {
'description' => __('Automatically approve new affiliate applications.', 'woonoow'),
'default' => false,
],
'woonoow_affiliate_enable_curated_collections' => [
'type' => 'toggle',
'label' => __('Enable Curated Collections', 'woonoow'),
'description' => __('Allow affiliates to create and share custom curated product collections.', 'woonoow'),
'default' => true,
],
'woonoow_affiliate_allow_self_referral' => [
'type' => 'toggle',
'label' => __('Allow Self-Referrals', 'woonoow'),

View File

@@ -102,9 +102,37 @@ class AffiliateTracker
'samesite' => 'Lax'
];
// Capture referral code
$referral_code = '';
// 1. Capture from ?ref= parameter
if (isset($_GET['ref']) && !empty($_GET['ref'])) {
$referral_code = sanitize_text_field($_GET['ref']);
}
// 2. Or capture from collection slug in URL (e.g., /collection/my-slug)
if (empty($referral_code)) {
$request_uri = $_SERVER['REQUEST_URI'] ?? '/';
$path = parse_url($request_uri, PHP_URL_PATH);
// Extract collection slug, accounting for possible subdirectories (e.g. /store/collection/slug)
if (preg_match('#/collection/([^/]+)#', $path, $matches)) {
$collection_slug = sanitize_text_field($matches[1]);
global $wpdb;
$collections_table = $wpdb->prefix . 'woonoow_affiliate_collections';
$affiliates_table = $wpdb->prefix . 'woonoow_affiliates';
$referral_code = $wpdb->get_var($wpdb->prepare("
SELECT a.referral_code
FROM $collections_table c
JOIN $affiliates_table a ON c.affiliate_id = a.id
WHERE c.slug = %s
", $collection_slug));
}
}
// Set the cookie if we found a referral code
if (!empty($referral_code)) {
$result = setcookie(self::COOKIE_NAME, $referral_code, $options);
$_COOKIE[self::COOKIE_NAME] = $referral_code;
error_log('[AffiliateTracker] Set woonoow_ref cookie: ' . $referral_code . ', result=' . ($result ? 'true' : 'false'));