feat: Affiliate program enrichment (Link Builder, Curated Collections, Smart Links)
This commit is contained in:
@@ -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'));
|
||||
|
||||
Reference in New Issue
Block a user