-> first-party cookie for the window (last click wins) add_action('init', function () { $id = ctp_click_id_from_request(); if ($id === '' || headers_sent()) { return; } setcookie('ctp_click', $id, array('expires' => time() + CTP_WINDOW_DAYS * 86400, 'path' => '/', 'secure' => is_ssl(), 'httponly' => false, 'samesite' => 'Lax')); $_COOKIE['ctp_click'] = $id; }, 1); // 2. the order remembers the click it came from // The through-us offer: when the shop declared a code at its claim and set CTP_COUPON beside its panel key, the code is // applied to the cart of a visitor who arrived from comparethepeptide (the ctp_click cookie), so nothing has to be typed. add_action('woocommerce_before_calculate_totals', function ($cart) { if (is_admin() || !defined('CTP_COUPON') || CTP_COUPON === '' || empty($_COOKIE['ctp_click'])) { return; } if (!$cart || $cart->has_discount(CTP_COUPON)) { return; } $cart->apply_coupon(CTP_COUPON); }, 20); add_action('woocommerce_checkout_order_created', function ($order) { $id = ctp_click_id_from_cookie(); if ($id === '' || !is_object($order)) { return; } $order->update_meta_data('_ctp_click', $id); $order->save(); }); // 3. server to server: paid / processing / completed -> reported; cancelled / refunded -> cancelled. Once per status. function ctp_report_order($order_id, $status) { if (!function_exists('wc_get_order') || !defined('CTP_PANEL_KEY') || CTP_PANEL_KEY === '') { return; } $order = wc_get_order($order_id); if (!$order) { return; } $ctp = (string) $order->get_meta('_ctp_click'); if (!preg_match('/^[0-9a-f]{12}$/', $ctp)) { return; } if ($order->get_meta('_ctp_reported_' . $status)) { return; } $body = array('key' => CTP_PANEL_KEY, 'order_ref' => (string) $order->get_order_number(), 'amount' => (float) $order->get_total(), 'currency' => (string) $order->get_currency(), 'ctp' => $ctp, 'status' => $status); $r = wp_remote_post(CTP_SALE_URL, array('timeout' => 8, 'headers' => array('Content-Type' => 'application/json'), 'body' => wp_json_encode($body))); $code = is_wp_error($r) ? 0 : (int) wp_remote_retrieve_response_code($r); if ($code >= 200 && $code < 300 || $code === 409) { // 409 = the panel already has it: the duty is met $order->update_meta_data('_ctp_reported_' . $status, gmdate('c') . ' http ' . $code); $order->save(); } else { $order->add_order_note('Compare The Peptide postback failed (http ' . $code . '); it is retried on the next status change.'); } } add_action('woocommerce_payment_complete', function ($id) { ctp_report_order($id, 'reported'); }); add_action('woocommerce_order_status_processing', function ($id) { ctp_report_order($id, 'reported'); }); add_action('woocommerce_order_status_completed', function ($id) { ctp_report_order($id, 'reported'); }); add_action('woocommerce_order_status_cancelled', function ($id) { ctp_report_order($id, 'cancelled'); }); add_action('woocommerce_order_status_refunded', function ($id) { ctp_report_order($id, 'cancelled'); }); // 4. the fall-back pixel on the thank-you page (the browser channel; the panel de-duplicates by order reference) add_action('woocommerce_thankyou', function ($order_id) { if (!function_exists('wc_get_order')) { return; } $order = wc_get_order($order_id); if (!$order) { return; } $ctp = (string) $order->get_meta('_ctp_click'); if (!preg_match('/^[0-9a-f]{12}$/', $ctp)) { return; } $domain = preg_replace('/^www\./', '', (string) parse_url(home_url(), PHP_URL_HOST)); $src = CTP_SALE_URL . '?pixel=1&domain=' . rawurlencode($domain) . '&order_ref=' . rawurlencode((string) $order->get_order_number()) . '&amount=' . rawurlencode((string) $order->get_total()) . '¤cy=' . rawurlencode((string) $order->get_currency()) . '&ctp=' . rawurlencode($ctp); echo ''; });