@@ -50,8 +50,9 @@ public function init_hooks() {
50
50
// Send events after checkout block.
51
51
add_action ( 'woocommerce_blocks_enqueue_checkout_block_scripts_after ' , array ( $ this , 'checkout_process ' ) );
52
52
53
- // order confirmed.
54
- add_action ( 'woocommerce_thankyou ' , array ( $ this , 'order_process ' ), 10 , 1 );
53
+ // order processed.
54
+ add_action ( 'woocommerce_checkout_order_processed ' , array ( $ this , 'order_process ' ), 10 , 1 );
55
+ add_action ( 'woocommerce_store_api_checkout_order_processed ' , array ( $ this , 'order_process ' ), 10 , 1 );
55
56
56
57
add_filter ( 'woocommerce_checkout_posted_data ' , array ( $ this , 'save_checkout_post_data ' ), 10 , 1 );
57
58
@@ -153,7 +154,7 @@ public function remove_from_cart() {
153
154
*/
154
155
public function capture_remove_from_cart ( $ cart_item_key , $ cart ) {
155
156
$ item = $ cart ->removed_cart_contents [ $ cart_item_key ] ?? null ;
156
- $ this ->capture_event_in_session_data ( (int ) $ item ['product_id ' ], (int ) $ item ['quantity ' ], ' woocommerceanalytics_remove_from_cart ' );
157
+ $ this ->capture_event_in_session_data ( ' woocommerceanalytics_remove_from_cart ' , (int ) $ item ['product_id ' ], (int ) $ item ['quantity ' ] );
157
158
}
158
159
159
160
/**
@@ -169,13 +170,13 @@ public function capture_remove_from_cart( $cart_item_key, $cart ) {
169
170
public function capture_cart_quantity_update ( $ cart_item_key , $ quantity , $ old_quantity , $ cart ) {
170
171
$ product_id = $ cart ->cart_contents [ $ cart_item_key ]['product_id ' ];
171
172
if ( $ quantity > $ old_quantity ) {
172
- $ this ->capture_event_in_session_data ( $ product_id , $ quantity, ' woocommerceanalytics_add_to_cart ' );
173
+ $ this ->capture_event_in_session_data ( ' woocommerceanalytics_add_to_cart ' , $ product_id , $ quantity );
173
174
$ this ->lock_add_to_cart_events = true ;
174
175
return ;
175
176
}
176
177
177
178
if ( $ quantity < $ old_quantity ) {
178
- $ this ->capture_event_in_session_data ( $ product_id , $ quantity, ' woocommerceanalytics_remove_from_cart ' );
179
+ $ this ->capture_event_in_session_data ( ' woocommerceanalytics_remove_from_cart ' , $ product_id , $ quantity );
179
180
return ;
180
181
}
181
182
}
@@ -291,12 +292,16 @@ public function checkout_process() {
291
292
}
292
293
293
294
/**
294
- * After the checkout process , fire an event for each item in the order
295
+ * After the order processed , fire an event for each item in the order
295
296
*
296
- * @param string $order_id Order Id.
297
+ * @param string|WC_Order $order_id_or_order Order Id or Order object .
297
298
*/
298
- public function order_process ( $ order_id ) {
299
- $ order = wc_get_order ( $ order_id );
299
+ public function order_process ( $ order_id_or_order ) {
300
+ if ( is_string ( $ order_id_or_order ) ) {
301
+ $ order = wc_get_order ( $ order_id_or_order );
302
+ } else {
303
+ $ order = $ order_id_or_order ;
304
+ }
300
305
301
306
if (
302
307
! $ order
@@ -359,8 +364,11 @@ public function order_process( $order_id ) {
359
364
if ( is_array ( $ order_coupons ) ) {
360
365
$ order_coupons_count = count ( $ order_coupons );
361
366
}
362
- $ this ->record_event (
367
+
368
+ $ this ->capture_event_in_session_data (
363
369
'woocommerceanalytics_product_purchase ' ,
370
+ $ product_id ,
371
+ $ order_item ->get_quantity (),
364
372
array (
365
373
'oi ' => $ order ->get_order_number (),
366
374
'pq ' => $ order_item ->get_quantity (),
@@ -379,8 +387,7 @@ public function order_process( $order_id ) {
379
387
'from_checkout ' => $ checkout_page_used ,
380
388
'checkout_page_contains_checkout_block ' => $ checkout_page_contains_checkout_block ,
381
389
'checkout_page_contains_checkout_shortcode ' => $ checkout_page_contains_checkout_shortcode ,
382
- ),
383
- $ product_id
390
+ )
384
391
);
385
392
}
386
393
}
@@ -455,23 +462,23 @@ public function capture_add_to_cart( $cart_item_key, $product_id, $quantity, $va
455
462
if ( $ this ->lock_add_to_cart_events ) {
456
463
return ;
457
464
}
458
- $ this ->capture_event_in_session_data ( $ product_id , $ quantity, ' woocommerceanalytics_add_to_cart ' );
465
+ $ this ->capture_event_in_session_data ( ' woocommerceanalytics_add_to_cart ' , $ product_id , $ quantity );
459
466
}
460
467
461
468
/**
462
469
* Track in-session data.
463
470
*
471
+ * @param string $event Fired event.
464
472
* @param int $product_id Product ID.
465
473
* @param int $quantity Quantity.
466
- * @param string $event Fired event .
474
+ * @param array $properties Event properties .
467
475
*/
468
- public function capture_event_in_session_data ( $ product_id , $ quantity , $ event ) {
476
+ public function capture_event_in_session_data ( $ event , $ product_id , $ quantity , $ properties = array () ) {
469
477
470
478
$ product = wc_get_product ( $ product_id );
471
479
if ( ! $ product instanceof WC_Product ) {
472
480
return ;
473
481
}
474
-
475
482
$ quantity = ( 0 === $ quantity ) ? 1 : $ quantity ;
476
483
477
484
// check for existing data.
@@ -484,14 +491,21 @@ public function capture_event_in_session_data( $product_id, $quantity, $event )
484
491
$ data = array ();
485
492
}
486
493
494
+ $ event_properties = array_merge (
495
+ array (
496
+ 'quantity ' => (string ) $ quantity ,
497
+ 'session_id ' => $ this ->get_session_id (),
498
+ 'landing_page ' => $ this ->get_landing_page (),
499
+ 'is_engaged ' => $ this ->is_engaged_session (),
500
+ ),
501
+ $ properties
502
+ );
503
+
487
504
// extract new event data.
488
505
$ new_data = array (
489
- 'event ' => $ event ,
490
- 'product_id ' => (string ) $ product_id ,
491
- 'quantity ' => (string ) $ quantity ,
492
- 'session_id ' => $ this ->get_session_id (),
493
- 'landing_page ' => $ this ->get_landing_page (),
494
- 'is_engaged ' => $ this ->is_engaged_session (),
506
+ 'event ' => $ event ,
507
+ 'product_id ' => (string ) $ product_id ,
508
+ 'properties ' => $ event_properties ,
495
509
);
496
510
497
511
// append new data.
0 commit comments