Introducde wc_load_cart to dynamically load the cart, this allows for functionality outside of frontend to initialise the cart.
This commit is contained in:
parent
196d5e77cb
commit
f6d9faa062
|
@ -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.
|
||||
*/
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -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 );
|
||||
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue