Skip to content

Apple Pay: Set order attribution data #783

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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' ] );

Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading