Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Commit

Permalink
Add output_order_received_page_view_properties on thank you page
Browse files Browse the repository at this point in the history
  • Loading branch information
opr authored and senadir committed Sep 19, 2023
1 parent 81633f9 commit 6676c83
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/Domain/Services/JetpackWooCommerceAnalytics.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,39 @@ public function get_product_details( $product ) {
);
}

/**
* Save the order received page view event properties to the asset data registry. The front end will consume these
* later.
*
* @param int $order_id The order ID.
*
* @return void
*/
public function output_order_received_page_view_properties( $order_id ) {
$order = wc_get_order( $order_id );
$product_data = wp_json_encode(
array_map(
function( $item ) {
$product = wc_get_product( $item->get_product_id() );
$product_details = $this->get_product_details( $product );
return array(
'pi' => $product_details['id'],
'pq' => $item->get_quantity(),
'pt' => $product_details['type'],
'pn' => $product_details['name'],
'pc' => $product_details['category'],
'pp' => $product_details['price'],
);
},
$order->get_items()
)
);

$properties = $this->get_cart_checkout_info();
$properties['products'] = $product_data;
$this->asset_data_registry->add( 'wc-blocks-jetpack-woocommerce-analytics_order_received_properties', $properties );
}

/**
* Check compatibility with Jetpack WooCommerce Analytics.
*
Expand All @@ -127,6 +160,7 @@ public function init_if_compatible() {
$this->register_assets();
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
add_action( 'wp_enqueue_scripts', array( $this, 'register_script_data' ) );
add_action( 'woocommerce_thankyou', array( $this, 'output_order_received_page_view_properties' ) );
}

/**
Expand Down

0 comments on commit 6676c83

Please sign in to comment.