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
This commit is contained in:
parent
75c18a6902
commit
310fd2c118
|
@ -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
|
|
@ -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.
|
||||
*
|
||||
|
|
Loading…
Reference in New Issue