diff --git a/includes/class-woocommerce.php b/includes/class-woocommerce.php index 164e980bb19..38451d0501a 100644 --- a/includes/class-woocommerce.php +++ b/includes/class-woocommerce.php @@ -546,17 +546,7 @@ final class WooCommerce { // Classes/actions loaded for the frontend and for ajax requests. if ( $this->is_request( 'frontend' ) ) { - // Session class, handles session data for users - can be overwritten if custom handler is needed. - $session_class = apply_filters( 'woocommerce_session_handler', 'WC_Session_Handler' ); - $this->session = new $session_class(); - $this->session->init(); - - $this->customer = new WC_Customer( get_current_user_id(), true ); - // Cart needs the customer info. - $this->cart = new WC_Cart(); - - // Customer should be saved during shutdown. - add_action( 'shutdown', array( $this->customer, 'save' ), 10 ); + wc_load_cart(); } $this->load_webhooks(); @@ -725,6 +715,39 @@ final class WooCommerce { wc_load_webhooks( 'active', $limit ); } + /** + * Initialize the customer and cart objects and setup customer saving on shutdown. + * + * @since 3.6.4 + * @return void + */ + public function initialize_cart() { + // Cart needs customer info. + if ( is_null( $this->customer ) || ! $this->customer instanceof WC_Customer ) { + $this->customer = new WC_Customer( get_current_user_id(), true ); + // Customer should be saved during shutdown. + add_action( 'shutdown', array( $this->customer, 'save' ), 10 ); + } + if ( is_null( $this->cart ) || ! $this->cart instanceof WC_Cart ) { + $this->cart = new WC_Cart(); + } + } + + /** + * Initialize the session class. + * + * @since 3.6.4 + * @return void + */ + public function initialize_session() { + // Session class, handles session data for users - can be overwritten if custom handler is needed. + $session_class = apply_filters( 'woocommerce_session_handler', 'WC_Session_Handler' ); + if ( is_null( $this->session ) || ! $this->session instanceof $session_class ) { + $this->session = new $session_class(); + $this->session->init(); + } + } + /** * Set tablenames inside WPDB object. */ diff --git a/includes/wc-core-functions.php b/includes/wc-core-functions.php index ee882453cf8..67d245dbcdf 100644 --- a/includes/wc-core-functions.php +++ b/includes/wc-core-functions.php @@ -2255,3 +2255,14 @@ function wc_get_server_database_version() { 'number' => preg_replace( '/([^\d.]+).*/', '', $server_info ), ); } + +/** + * Initialize and load the cart functionality. + * + * @since 3.6.4 + * @return void + */ +function wc_load_cart() { + WC()->initialize_session(); + WC()->initialize_cart(); +} diff --git a/tests/unit-tests/util/class-wc-tests-core-functions.php b/tests/unit-tests/util/class-wc-tests-core-functions.php index 6e997f63b6e..707075f83ea 100644 --- a/tests/unit-tests/util/class-wc-tests-core-functions.php +++ b/tests/unit-tests/util/class-wc-tests-core-functions.php @@ -10,6 +10,17 @@ * Core function unit tests. */ class WC_Tests_Core_Functions extends WC_Unit_Test_Case { + + /** + * Set up test + * + * @return void + */ + public function setUp() { + parent::setUp(); + $this->wc = WC(); + } + /** * Test get_woocommerce_currency(). * @@ -932,4 +943,26 @@ class WC_Tests_Core_Functions extends WC_Unit_Test_Case { uasort( $sorted_values, 'wc_ascii_uasort_comparison' ); $this->assertSame( array( 'BĂ©lgica', 'Benin' ), array_values( $sorted_values ) ); } + + /** + * Test wc_load_cart function. + * + * @return void + */ + public function test_wc_load_cart() { + $this->assertInstanceOf( 'WC_Cart', $this->wc->cart ); + $this->assertInstanceOf( 'WC_Customer', $this->wc->customer ); + $this->assertInstanceOf( 'WC_Session', $this->wc->session ); + + $this->wc->cart = $this->wc->customer = $this->wc->session = null; + $this->assertNull( $this->wc->cart ); + $this->assertNull( $this->wc->customer ); + $this->assertNull( $this->wc->session ); + + wc_load_cart(); + $this->assertInstanceOf( 'WC_Cart', $this->wc->cart ); + $this->assertInstanceOf( 'WC_Customer', $this->wc->customer ); + $this->assertInstanceOf( 'WC_Session', $this->wc->session ); + + } }