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..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 @@ -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,54 @@ 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; + } + + try { + $container = wc_get_container(); + + $featureController = $container->get( \Automattic\WooCommerce\Internal\Features\FeaturesController::class ); + + if (! $featureController->is_feature_active( 'order_attribution' )) { + $this->log( '-- Order attribution feature not active' ); + return; + } + + $orderAttributionController = $container->get( \Automattic\WooCommerce\Internal\Orders\OrderAttributionController::class ); + + // 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. + * + * @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..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 @@ -310,6 +310,12 @@ jQuery ( $ ) -> payment: JSON.stringify( payment ) } + # 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 ) => if response.success