From b570e707c6c6f9197bbf55486f4cb04954320638 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20P=C3=A9rez=20Pellicer?= <5908855+puntope@users.noreply.github.com> Date: Tue, 24 Dec 2024 17:26:44 +0100 Subject: [PATCH 1/2] Prevent fatal when cat is null --- .../src/class-woo-analytics-trait.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/projects/packages/woocommerce-analytics/src/class-woo-analytics-trait.php b/projects/packages/woocommerce-analytics/src/class-woo-analytics-trait.php index b7bcf0b3446f4..d726bbf8ed1fc 100644 --- a/projects/packages/woocommerce-analytics/src/class-woo-analytics-trait.php +++ b/projects/packages/woocommerce-analytics/src/class-woo-analytics-trait.php @@ -585,6 +585,9 @@ public function post_contains_text( $post_id, $text ) { */ public function get_cart_total() { $cart = WC()->cart; + if ( $cart === null ) { + return 0; + } return $cart->get_total( 'tracking' ); } @@ -595,6 +598,9 @@ public function get_cart_total() { */ public function get_cart_subtotal() { $cart = WC()->cart; + if ( $cart === null ) { + return 0; + } return $cart->get_subtotal(); } @@ -605,6 +611,9 @@ public function get_cart_subtotal() { */ public function get_cart_shipping_total() { $cart = WC()->cart; + if ( $cart === null ) { + return 0; + } return $cart->get_shipping_total(); } @@ -615,6 +624,9 @@ public function get_cart_shipping_total() { */ public function get_cart_taxes() { $cart = WC()->cart; + if ( $cart === null ) { + return 0; + } return $cart->get_taxes_total(); } @@ -625,6 +637,9 @@ public function get_cart_taxes() { */ public function get_total_discounts() { $cart = WC()->cart; + if ( $cart === null ) { + return 0; + } return $cart->get_discount_total(); } @@ -635,6 +650,9 @@ public function get_total_discounts() { */ public function get_cart_items_count() { $cart = WC()->cart; + if ( $cart === null ) { + return 0; + } return $cart->get_cart_contents_count(); } } From e19c24e84c5549d9bddc3989154fc513d2b7eb58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20P=C3=A9rez=20Pellicer?= <5908855+puntope@users.noreply.github.com> Date: Tue, 24 Dec 2024 17:28:18 +0100 Subject: [PATCH 2/2] Changelog --- .../changelog/fix-analytics-error-cart-null | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 projects/packages/woocommerce-analytics/changelog/fix-analytics-error-cart-null diff --git a/projects/packages/woocommerce-analytics/changelog/fix-analytics-error-cart-null b/projects/packages/woocommerce-analytics/changelog/fix-analytics-error-cart-null new file mode 100644 index 0000000000000..3d5c182ebdbef --- /dev/null +++ b/projects/packages/woocommerce-analytics/changelog/fix-analytics-error-cart-null @@ -0,0 +1,4 @@ +Significance: patch +Type: fixed + +Fix fatal when WC()->cart returns null