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. *