From 5679a674f296a669443f88b177339556b2139492 Mon Sep 17 00:00:00 2001 From: Ashley Gibson Date: Fri, 13 Jun 2025 14:14:33 +0100 Subject: [PATCH 1/2] Try to set order attribution data --- .../class-sv-wc-payment-gateway-apple-pay.php | 43 +++++++++++++++++++ .../sv-wc-payment-gateway-apple-pay.coffee | 4 ++ 2 files changed, 47 insertions(+) diff --git a/woocommerce/payment-gateway/External_Checkout/apple-pay/class-sv-wc-payment-gateway-apple-pay.php b/woocommerce/payment-gateway/External_Checkout/apple-pay/class-sv-wc-payment-gateway-apple-pay.php index 6b2106f19..56d11c659 100755 --- a/woocommerce/payment-gateway/External_Checkout/apple-pay/class-sv-wc-payment-gateway-apple-pay.php +++ b/woocommerce/payment-gateway/External_Checkout/apple-pay/class-sv-wc-payment-gateway-apple-pay.php @@ -158,6 +158,8 @@ public function process_payment() { $order->save(); + $this->maybeAddAttributionData($order); + // add Apple Pay response data to the order add_filter( 'wc_payment_gateway_' . $this->get_processing_gateway()->get_id() . '_get_order', [ $this, 'add_order_data' ] ); @@ -194,6 +196,47 @@ public function process_payment() { } } + protected function maybeAddAttributionData(\WC_Order $order) : void + { + $attributionData = SV_WC_Helper::get_posted_value( 'order_attribution' ); + if ( empty( $attributionData ) ) { + return; + } + + if (! class_exists( \Automattic\WooCommerce\Internal\Orders\OrderAttributionController::class )) { + return; + } + + try { + $container = wc_get_container(); + + $featureController = $container->get( \Automattic\WooCommerce\Internal\Features\FeaturesController::class ); + + if (! $featureController->is_feature_active( 'order_attribution' )) { + return; + } + + $orderAttributionController = $container->get( \Automattic\WooCommerce\Internal\Orders\OrderAttributionController::class ); + + // bail if the order already has attribution + if ($orderAttributionController->has_attribution($order)) { + return; + } + + /** + * Run an action to save order attribution data. + * + * @see \Automattic\WooCommerce\Internal\Orders\OrderAttributionController::on_init() + * + * @param WC_Order $order The order object. + * @param array $params Unprefixed order attribution data. + */ + do_action( 'woocommerce_order_save_attribution_data', $order, $attributionData ); + } catch ( \Exception $e ) { + $this->log( 'Error adding attribution data to order: ' . $e->getMessage() ); + } + } + /** * Updates a customer's stored billing & shipping addresses based on the diff --git a/woocommerce/payment-gateway/assets/js/frontend/sv-wc-payment-gateway-apple-pay.coffee b/woocommerce/payment-gateway/assets/js/frontend/sv-wc-payment-gateway-apple-pay.coffee index 55868c9c8..e3ba57779 100644 --- a/woocommerce/payment-gateway/assets/js/frontend/sv-wc-payment-gateway-apple-pay.coffee +++ b/woocommerce/payment-gateway/assets/js/frontend/sv-wc-payment-gateway-apple-pay.coffee @@ -310,6 +310,10 @@ jQuery ( $ ) -> payment: JSON.stringify( payment ) } + # Add order attribution data if available + if window?.wc_order_attribution + data.order_attribution = wc_order_attribution.getAttributionData() + $.post @ajax_url, data, ( response ) => if response.success From aa3637ec69d042b68d273c11755eaa3b55426d77 Mon Sep 17 00:00:00 2001 From: Ashley Gibson Date: Fri, 13 Jun 2025 14:15:51 +0100 Subject: [PATCH 2/2] Add logging to aid with debugging --- .../apple-pay/class-sv-wc-payment-gateway-apple-pay.php | 7 +++++++ .../js/frontend/sv-wc-payment-gateway-apple-pay.coffee | 2 ++ 2 files changed, 9 insertions(+) diff --git a/woocommerce/payment-gateway/External_Checkout/apple-pay/class-sv-wc-payment-gateway-apple-pay.php b/woocommerce/payment-gateway/External_Checkout/apple-pay/class-sv-wc-payment-gateway-apple-pay.php index 56d11c659..a5d7fb421 100755 --- a/woocommerce/payment-gateway/External_Checkout/apple-pay/class-sv-wc-payment-gateway-apple-pay.php +++ b/woocommerce/payment-gateway/External_Checkout/apple-pay/class-sv-wc-payment-gateway-apple-pay.php @@ -198,12 +198,15 @@ public function process_payment() { protected function maybeAddAttributionData(\WC_Order $order) : void { + $this->log( 'Maybe adding attribution data to order' ); $attributionData = SV_WC_Helper::get_posted_value( 'order_attribution' ); if ( empty( $attributionData ) ) { + $this->log( '-- No attribution data found' ); return; } if (! class_exists( \Automattic\WooCommerce\Internal\Orders\OrderAttributionController::class )) { + $this->log( '-- Order attribution controller not found' ); return; } @@ -213,6 +216,7 @@ protected function maybeAddAttributionData(\WC_Order $order) : void $featureController = $container->get( \Automattic\WooCommerce\Internal\Features\FeaturesController::class ); if (! $featureController->is_feature_active( 'order_attribution' )) { + $this->log( '-- Order attribution feature not active' ); return; } @@ -220,9 +224,12 @@ protected function maybeAddAttributionData(\WC_Order $order) : void // bail if the order already has attribution if ($orderAttributionController->has_attribution($order)) { + $this->log( '-- Order already has attribution' ); return; } + $this->log( '-- Adding attribution data to order' ); + /** * Run an action to save order attribution data. * diff --git a/woocommerce/payment-gateway/assets/js/frontend/sv-wc-payment-gateway-apple-pay.coffee b/woocommerce/payment-gateway/assets/js/frontend/sv-wc-payment-gateway-apple-pay.coffee index e3ba57779..1706cd30e 100644 --- a/woocommerce/payment-gateway/assets/js/frontend/sv-wc-payment-gateway-apple-pay.coffee +++ b/woocommerce/payment-gateway/assets/js/frontend/sv-wc-payment-gateway-apple-pay.coffee @@ -313,6 +313,8 @@ jQuery ( $ ) -> # Add order attribution data if available if window?.wc_order_attribution data.order_attribution = wc_order_attribution.getAttributionData() + else + console.log 'No order attribution data found' $.post @ajax_url, data, ( response ) =>