From a3dccfdc50801acba98f42f34bcdbfe93d690544 Mon Sep 17 00:00:00 2001 From: Boro Sitnikovski Date: Mon, 9 Dec 2024 16:35:25 +0100 Subject: [PATCH 1/7] Use reliable way to detect checkout page --- .../packages/woocommerce-analytics/src/class-checkout-flow.php | 3 +-- .../packages/woocommerce-analytics/src/class-universal.php | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/projects/packages/woocommerce-analytics/src/class-checkout-flow.php b/projects/packages/woocommerce-analytics/src/class-checkout-flow.php index 9d7422f530d17..94dd3dd3ebeb4 100644 --- a/projects/packages/woocommerce-analytics/src/class-checkout-flow.php +++ b/projects/packages/woocommerce-analytics/src/class-checkout-flow.php @@ -154,7 +154,6 @@ public function capture_checkout_view() { return; } - $is_in_checkout_page = $checkout_page_id === $post->ID ? 'Yes' : 'No'; $checkout_page_contains_checkout_block = '0'; $checkout_page_contains_checkout_shortcode = '1'; @@ -179,7 +178,7 @@ public function capture_checkout_view() { array_merge( $this->get_cart_checkout_shared_data(), array( - 'from_checkout' => $is_in_checkout_page, + 'from_checkout' => 'Yes', 'checkout_page_contains_checkout_block' => $checkout_page_contains_checkout_block, 'checkout_page_contains_checkout_shortcode' => $checkout_page_contains_checkout_shortcode, ) diff --git a/projects/packages/woocommerce-analytics/src/class-universal.php b/projects/packages/woocommerce-analytics/src/class-universal.php index 5a8fb6587b309..f5e647e02026f 100644 --- a/projects/packages/woocommerce-analytics/src/class-universal.php +++ b/projects/packages/woocommerce-analytics/src/class-universal.php @@ -198,7 +198,7 @@ function ( $payment_gateway ) { $enabled_payment_options = array_keys( $enabled_payment_options ); - $is_in_checkout_page = $checkout_page_id === $post->ID ? 'Yes' : 'No'; + $is_in_checkout_page = $checkout_page_id && is_page( $checkout_page_id ) ? 'Yes' : 'No'; $session = WC()->session; if ( is_object( $session ) ) { $session->set( 'checkout_page_used', true ); From 225182e0cf7bcedd56e6f1409d76e00f83b9e468 Mon Sep 17 00:00:00 2001 From: Boro Sitnikovski Date: Mon, 9 Dec 2024 16:41:04 +0100 Subject: [PATCH 2/7] Changelog --- .../changelog/fix-php-warnings-for-post-id | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 projects/packages/woocommerce-analytics/changelog/fix-php-warnings-for-post-id diff --git a/projects/packages/woocommerce-analytics/changelog/fix-php-warnings-for-post-id b/projects/packages/woocommerce-analytics/changelog/fix-php-warnings-for-post-id new file mode 100644 index 0000000000000..d4ff87f06df4e --- /dev/null +++ b/projects/packages/woocommerce-analytics/changelog/fix-php-warnings-for-post-id @@ -0,0 +1,5 @@ +Significance: patch +Type: added + +Use reliable way to detect checkout page + From ab4e9524f536b4eeb54a5d3ffbdcded55b51a252 Mon Sep 17 00:00:00 2001 From: Boro Sitnikovski Date: Mon, 9 Dec 2024 17:08:32 +0100 Subject: [PATCH 3/7] Lint --- projects/packages/woocommerce-analytics/src/class-universal.php | 1 - 1 file changed, 1 deletion(-) diff --git a/projects/packages/woocommerce-analytics/src/class-universal.php b/projects/packages/woocommerce-analytics/src/class-universal.php index f5e647e02026f..fe3bde52d787e 100644 --- a/projects/packages/woocommerce-analytics/src/class-universal.php +++ b/projects/packages/woocommerce-analytics/src/class-universal.php @@ -181,7 +181,6 @@ public function get_shipping_option_for_item( $cart_item_key ) { * On the Checkout page, trigger an event for each product in the cart */ public function checkout_process() { - global $post; $checkout_page_id = wc_get_page_id( 'checkout' ); $cart = WC()->cart->get_cart(); From eee295d54dfd5cac760939f62284562f8734f25a Mon Sep 17 00:00:00 2001 From: Boro Sitnikovski Date: Mon, 9 Dec 2024 18:09:42 +0100 Subject: [PATCH 4/7] Return early when post is undefined --- .../woocommerce-analytics/src/class-checkout-flow.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/projects/packages/woocommerce-analytics/src/class-checkout-flow.php b/projects/packages/woocommerce-analytics/src/class-checkout-flow.php index 94dd3dd3ebeb4..bae2b70a4064a 100644 --- a/projects/packages/woocommerce-analytics/src/class-checkout-flow.php +++ b/projects/packages/woocommerce-analytics/src/class-checkout-flow.php @@ -141,6 +141,11 @@ public function capture_cart_view() { */ public function capture_checkout_view() { global $post; + + if ( ! $post ) { + return; + } + $checkout_page_id = wc_get_page_id( 'checkout' ); $is_checkout = $checkout_page_id && is_page( $checkout_page_id ) From 7d1307ed4f6844972ecf00fb2f214736f5e99e5e Mon Sep 17 00:00:00 2001 From: Boro Sitnikovski Date: Mon, 9 Dec 2024 18:41:13 +0100 Subject: [PATCH 5/7] Update projects/packages/woocommerce-analytics/src/class-checkout-flow.php Co-authored-by: Jeremy Herve --- .../packages/woocommerce-analytics/src/class-checkout-flow.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/projects/packages/woocommerce-analytics/src/class-checkout-flow.php b/projects/packages/woocommerce-analytics/src/class-checkout-flow.php index bae2b70a4064a..e9fac2b12b15b 100644 --- a/projects/packages/woocommerce-analytics/src/class-checkout-flow.php +++ b/projects/packages/woocommerce-analytics/src/class-checkout-flow.php @@ -142,7 +142,7 @@ public function capture_cart_view() { public function capture_checkout_view() { global $post; - if ( ! $post ) { + if ( ! $post instanceof WP_Post ) { return; } From 513b0cd73c9cbd647ff783302b8870556d5b7845 Mon Sep 17 00:00:00 2001 From: Jeremy Herve Date: Mon, 9 Dec 2024 19:54:00 +0100 Subject: [PATCH 6/7] Fix namespace issue introduced in 7d1307ed4f6844972ecf00fb2f214736f5e99e5e --- .../packages/woocommerce-analytics/src/class-checkout-flow.php | 1 + 1 file changed, 1 insertion(+) diff --git a/projects/packages/woocommerce-analytics/src/class-checkout-flow.php b/projects/packages/woocommerce-analytics/src/class-checkout-flow.php index e9fac2b12b15b..236b1b1c6dec7 100644 --- a/projects/packages/woocommerce-analytics/src/class-checkout-flow.php +++ b/projects/packages/woocommerce-analytics/src/class-checkout-flow.php @@ -9,6 +9,7 @@ use Automattic\Jetpack\Constants; use WC_Product; +use WP_Post; /** * Class that handles all page view events for the checkout flow (from product view to order confirmation view) From 14577857e75f6cdb778115e414830b7398627e80 Mon Sep 17 00:00:00 2001 From: Boro Sitnikovski Date: Thu, 19 Dec 2024 13:09:26 +0100 Subject: [PATCH 7/7] Add similar check to class-universal --- .../packages/woocommerce-analytics/src/class-universal.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/projects/packages/woocommerce-analytics/src/class-universal.php b/projects/packages/woocommerce-analytics/src/class-universal.php index 03f8a7548660b..511fe3b8f20e9 100644 --- a/projects/packages/woocommerce-analytics/src/class-universal.php +++ b/projects/packages/woocommerce-analytics/src/class-universal.php @@ -9,6 +9,7 @@ use WC_Order; use WC_Product; +use WP_Post; /** * Filters and Actions added to Store pages to perform analytics @@ -209,6 +210,12 @@ public function get_shipping_option_for_item( $cart_item_key ) { * On the Checkout page, trigger an event for each product in the cart */ public function checkout_process() { + global $post; + + if ( ! $post instanceof WP_Post ) { + return; + } + $checkout_page_id = wc_get_page_id( 'checkout' ); $cart = WC()->cart->get_cart(); $is_in_checkout_page = $checkout_page_id && is_page( $checkout_page_id ) ? 'Yes' : 'No';