From 310fd2c11833e279ea5a001c7aed38fda605af9c Mon Sep 17 00:00:00 2001 From: Corey McKrill <916023+coreymckrill@users.noreply.github.com> Date: Wed, 14 Aug 2024 14:12:37 -0700 Subject: [PATCH] Ensure WC's customer and cart props are initialized If the customer or cart property of the main WooCommerce class is accessed before the `woocommerce_init` action has fired, those properties will be `null` instead of containing their respective classes, which can cause a fatal error if you then try to access a method on one of the classes. This makes sure the props will always return a class. Fixes #50245 --- .../fix-50245-fatal-error-checkout-customer | 4 +++ .../includes/class-woocommerce.php | 30 +++++++++++++++++-- 2 files changed, 31 insertions(+), 3 deletions(-) create mode 100644 plugins/woocommerce/changelog/fix-50245-fatal-error-checkout-customer diff --git a/plugins/woocommerce/changelog/fix-50245-fatal-error-checkout-customer b/plugins/woocommerce/changelog/fix-50245-fatal-error-checkout-customer new file mode 100644 index 00000000000..28a5c165430 --- /dev/null +++ b/plugins/woocommerce/changelog/fix-50245-fatal-error-checkout-customer @@ -0,0 +1,4 @@ +Significance: minor +Type: fix + +Address a fatal error that could happen if certain properties of the main WooCommerce class were accessed before WooCommerce had finished initializing diff --git a/plugins/woocommerce/includes/class-woocommerce.php b/plugins/woocommerce/includes/class-woocommerce.php index a8949206d52..09e97597af8 100644 --- a/plugins/woocommerce/includes/class-woocommerce.php +++ b/plugins/woocommerce/includes/class-woocommerce.php @@ -113,14 +113,14 @@ final class WooCommerce { * * @var WC_Cart */ - public $cart = null; + private $cart = null; /** * Customer instance. * * @var WC_Customer */ - public $customer = null; + private $customer = null; /** * Order factory instance. @@ -199,7 +199,7 @@ final class WooCommerce { return $this->api; } - if ( in_array( $key, array( 'payment_gateways', 'shipping', 'mailer', 'checkout' ), true ) ) { + if ( in_array( $key, array( 'payment_gateways', 'shipping', 'mailer', 'checkout', 'customer', 'cart' ), true ) ) { return $this->$key(); } } @@ -213,6 +213,8 @@ final class WooCommerce { public function __set( string $key, $value ) { if ( 'api' === $key ) { $this->api = $value; + } elseif ( in_array( $key, array( 'customer', 'cart' ), true ) ) { + $this->$key = $value; } elseif ( property_exists( $this, $key ) ) { // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_trigger_error trigger_error( 'Cannot access private property WooCommerce::$' . esc_html( $key ), E_USER_ERROR ); @@ -1154,6 +1156,28 @@ final class WooCommerce { return WC_Shipping::instance(); } + /** + * Get customer class. + * + * @return WC_Customer + */ + public function customer() { + $this->initialize_cart(); + + return $this->customer; + } + + /** + * Get cart class. + * + * @return WC_Cart + */ + public function cart() { + $this->initialize_cart(); + + return $this->cart; + } + /** * Email Class. *