From 75ea3f547241912e151d79a17c20ea14fdb207d8 Mon Sep 17 00:00:00 2001 From: amisha25-razor Date: Tue, 1 Apr 2025 12:37:55 +0530 Subject: [PATCH] Fix: Ensure WooCommerce order attribution tracking saves properly --- includes/api/order.php | 61 +++++++++++++++++++++++++++--------------- 1 file changed, 40 insertions(+), 21 deletions(-) diff --git a/includes/api/order.php b/includes/api/order.php index 15ebb312..96d774e3 100644 --- a/includes/api/order.php +++ b/includes/api/order.php @@ -127,12 +127,13 @@ function createWcOrder(WP_REST_Request $request) } // Pixel your site PRO UTM data - if (is_plugin_active('pixelyoursite-pro/pixelyoursite-pro.php')) { - + if (is_plugin_active('pixelyoursite-pro/pixelyoursite-pro.php') || is_plugin_active('pixelyoursite/facebook-pixel-master.php')) { + rzpLogInfo("pixelyoursite-pro/pixelyoursite-pro.php is activated for order id-". $orderId); $pysData = get_option('pys_core'); // Store UTM data only if config enabled. if ($pysData['woo_enabled_save_data_to_orders'] == true) { + rzpLogInfo("woo_enabled_save_data_to_orders value is ".$pysData['woo_enabled_save_data_to_orders']); wooSaveCheckoutUTMFields($order, $params); } } @@ -275,29 +276,47 @@ function updateOrderStatus($orderId, $orderStatus) function wooSaveCheckoutUTMFields($order, $params) { - $pysData = []; - $cookieData = $params['cookies']; + if (!$order) return; $getQuery = $params['requestData']; $browserTime = $params['dateTime']; - $pysData['pys_landing'] = isset($cookieData['pys_landing_page']) ? ($cookieData['pys_landing_page']) : ""; - $pysData['pys_source'] = isset($cookieData['pysTrafficSource']) ? ($cookieData['pysTrafficSource']) : "direct"; - if ($pysData['pys_source'] == 'direct') { - $pysData['pys_source'] = $params['referrerDomain'] != '' ? $params['referrerDomain'] : "direct"; + rzpLogInfo('Razorpay Log: sbjs_current - ' . json_encode($_COOKIE['sbjs_current'])); + $data = []; + + // Get data from Sourcebuster.js (sbjs) cookies + if (!empty($_COOKIE['sbjs_current'])) { + parse_str(str_replace('|', '&', $_COOKIE['sbjs_current']), $sbjs_current); + $data['source_type'] = $sbjs_current['typ'] ?? 'direct'; + $data['utm_source'] = $sbjs_current['src'] ?? '(direct)'; + $data['utm_medium'] = $sbjs_current['mdm'] ?? '(none)'; + $data['utm_campaign'] = $sbjs_current['cmp'] ?? '(none)'; + $data['referrer'] = $sbjs_current['rf'] ?? ''; } - $pysUTMSource = $cookieData['pys_utm_source'] ?? $getQuery['utm_source']; - $pysUTMMedium = $cookieData['pys_utm_medium'] ?? $getQuery['utm_medium']; - $pysUTMCampaign = $cookieData['pys_utm_campaign'] ?? $getQuery['utm_medium']; - $pysUTMTerm = $cookieData['pys_utm_term'] ?? $getQuery['utm_term']; - $pysUTMContent = $cookieData['pys_utm_content'] ?? $getQuery['utm_content']; - $pysData['pys_utm'] = "utm_source:" . $pysUTMSource . "|utm_medium:" . $pysUTMMedium . "|utm_campaign:" . $pysUTMCampaign . "|utm_term:" . $pysUTMTerm . "|utm_content:" . $pysUTMContent; - $pysData['pys_browser_time'] = $browserTime[0] . "|" . $browserTime[1] . "|" . $browserTime[2]; + // Get data from WooCommerce Order Attribution Script (wc_order_attribution) + if (!empty($_COOKIE['wc_order_attribution'])) { + $wc_order_attribution = json_decode(stripslashes($_COOKIE['wc_order_attribution']), true); + if (!empty($wc_order_attribution['fields'])) { + foreach ($wc_order_attribution['fields'] as $key => $cookie_key) { + if (isset($_COOKIE[$cookie_key])) { + $data[$key] = $_COOKIE[$cookie_key]; + } + } + } + } - if (isHposEnabled()) { - $order->update_meta_data( 'pys_enrich_data', $pysData ); - $order->save(); - }else{ - update_post_meta($order->get_id(), "pys_enrich_data", $pysData); + // Capture referrer from HTTP headers (fallback) + if (empty($data['referrer']) && !empty($_SERVER['HTTP_REFERER'])) { + $data['referrer'] = $_SERVER['HTTP_REFERER']; + } + + // Capture user agent + $data['user_agent'] = $_SERVER['HTTP_USER_AGENT'] ?? ''; + rzpLogInfo('Store Cookie Data - ' . json_encode($data)); + + //Save each key-value pair as order meta (WooCommerce format) + foreach ($data as $key => $value) { + $order->update_meta_data('_wc_order_attribution_' . $key, $value); } - + $order->save(); + }