Defer cart cookie being set

This commit is contained in:
Mike Jolley 2014-06-30 15:14:32 +01:00
parent 779613b062
commit 137bf945ab
1 changed files with 34 additions and 22 deletions

View File

@ -114,6 +114,7 @@ class WC_Cart {
); );
add_action( 'init', array( $this, 'init' ), 5 ); // Get cart on init add_action( 'init', array( $this, 'init' ), 5 ); // Get cart on init
add_action( 'wp', array( $this, 'maybe_set_cart_cookies' ), 99 ); // Set cookies
} }
/** /**
@ -130,6 +131,35 @@ class WC_Cart {
add_action( 'woocommerce_after_checkout_validation', array( $this, 'check_customer_coupons' ), 1 ); add_action( 'woocommerce_after_checkout_validation', array( $this, 'check_customer_coupons' ), 1 );
} }
/**
* Will set cart cookies if needed, once, during WP hook
*/
public function maybe_set_cart_cookies() {
if ( sizeof( $this->cart_contents ) > 0 ) {
$this->set_cart_cookies( true );
} elseif ( isset( $_COOKIE['woocommerce_items_in_cart'] ) ) {
$this->set_cart_cookies( false );
}
}
/**
* Set cart hash cookie and items in cart.
*
* @access private
* @param bool $set (default: true)
* @return void
*/
private function set_cart_cookies( $set = true ) {
if ( $set ) {
wc_setcookie( 'woocommerce_items_in_cart', 1 );
wc_setcookie( 'woocommerce_cart_hash', md5( json_encode( $this->get_cart() ) ) );
} elseif ( isset( $_COOKIE['woocommerce_items_in_cart'] ) ) {
wc_setcookie( 'woocommerce_items_in_cart', 0, time() - 3600 );
wc_setcookie( 'woocommerce_cart_hash', '', time() - 3600 );
}
do_action( 'woocommerce_set_cart_cookies', $set );
}
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
/* Cart Session Handling */ /* Cart Session Handling */
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
@ -187,8 +217,6 @@ class WC_Cart {
WC()->session->cart = $this->get_cart_for_session(); WC()->session->cart = $this->get_cart_for_session();
} }
$this->set_cart_cookies( sizeof( $this->cart_contents ) > 0 );
// Trigger action // Trigger action
do_action( 'woocommerce_cart_loaded_from_session', $this ); do_action( 'woocommerce_cart_loaded_from_session', $this );
@ -891,9 +919,12 @@ class WC_Cart {
} }
if ( did_action( 'wp' ) ) {
$this->set_cart_cookies( sizeof( $this->cart_contents ) > 0 );
}
do_action( 'woocommerce_add_to_cart', $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data ); do_action( 'woocommerce_add_to_cart', $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data );
$this->set_cart_cookies();
$this->calculate_totals(); $this->calculate_totals();
return true; return true;
@ -920,25 +951,6 @@ class WC_Cart {
} }
} }
/**
* Set cart hash cookie and items in cart.
*
* @access private
* @param bool $set (default: true)
* @return void
*/
private function set_cart_cookies( $set = true ) {
if ( $set ) {
wc_setcookie( 'woocommerce_items_in_cart', 1 );
wc_setcookie( 'woocommerce_cart_hash', md5( json_encode( $this->get_cart() ) ) );
} elseif ( isset( $_COOKIE['woocommerce_items_in_cart'] ) ) {
wc_setcookie( 'woocommerce_items_in_cart', 0, time() - 3600 );
wc_setcookie( 'woocommerce_cart_hash', '', time() - 3600 );
}
do_action( 'woocommerce_set_cart_cookies', $set );
}
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
/* Cart Calculation Functions */ /* Cart Calculation Functions */
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/