From 70ea2940bafb5f40939ca8feecc04044f814f1c6 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 21:11:16 +0400 Subject: [PATCH] [WC Analytics] Fix fatal error when cart is null (#40729) * Prevent fatal when cat is null * Changelog --- .../changelog/fix-analytics-error-cart-null | 4 ++++ .../src/class-woo-analytics-trait.php | 18 ++++++++++++++++++ 2 files changed, 22 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 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(); } }