Load persistent cart when empty, and Prevent cart being cleared when accessing the login page.

Closes #7636
This commit is contained in:
Mike Jolley 2015-03-06 12:01:58 +00:00
parent 25944c9086
commit 9f269f765d
4 changed files with 20 additions and 20 deletions

View File

@ -202,19 +202,26 @@ class WC_Cart {
* Get the cart data from the PHP session and store it in class variables. * Get the cart data from the PHP session and store it in class variables.
*/ */
public function get_cart_from_session() { public function get_cart_from_session() {
// Load cart session data from session // Load cart session data from session
foreach ( $this->cart_session_data as $key => $default ) { foreach ( $this->cart_session_data as $key => $default ) {
$this->$key = WC()->session->get( $key, $default ); $this->$key = WC()->session->get( $key, $default );
} }
$update_cart_session = false;
$this->removed_cart_contents = array_filter( WC()->session->get( 'removed_cart_contents', array() ) ); $this->removed_cart_contents = array_filter( WC()->session->get( 'removed_cart_contents', array() ) );
$this->applied_coupons = array_filter( WC()->session->get( 'applied_coupons', array() ) ); $this->applied_coupons = array_filter( WC()->session->get( 'applied_coupons', array() ) );
// Load the cart /**
$cart = WC()->session->get( 'cart', array() ); * Load the cart object. This defaults to the persistant cart if null.
*/
$cart = WC()->session->get( 'cart', null );
$update_cart_session = false; if ( is_null( $cart ) && ( $saved_cart = get_user_meta( get_current_user_id(), '_woocommerce_persistent_cart', true ) ) ) {
$cart = $saved_cart['cart'];
$update_cart_session = true;
} elseif ( is_null( $cart ) ) {
$cart = array();
}
if ( is_array( $cart ) ) { if ( is_array( $cart ) ) {
foreach ( $cart as $key => $values ) { foreach ( $cart as $key => $values ) {
@ -304,7 +311,7 @@ class WC_Cart {
*/ */
public function persistent_cart_update() { public function persistent_cart_update() {
update_user_meta( get_current_user_id(), '_woocommerce_persistent_cart', array( update_user_meta( get_current_user_id(), '_woocommerce_persistent_cart', array(
'cart' => WC()->session->cart, 'cart' => WC()->session->get( 'cart' )
) ); ) );
} }

View File

@ -67,7 +67,7 @@ class WC_Session_Handler extends WC_Session {
add_action( 'woocommerce_set_cart_cookies', array( $this, 'set_customer_session_cookie' ), 10 ); add_action( 'woocommerce_set_cart_cookies', array( $this, 'set_customer_session_cookie' ), 10 );
add_action( 'woocommerce_cleanup_sessions', array( $this, 'cleanup_sessions' ), 10 ); add_action( 'woocommerce_cleanup_sessions', array( $this, 'cleanup_sessions' ), 10 );
add_action( 'shutdown', array( $this, 'save_data' ), 20 ); add_action( 'shutdown', array( $this, 'save_data' ), 20 );
add_action( 'clear_auth_cookie', array( $this, 'destroy_session' ) ); add_action( 'wp_logout', array( $this, 'destroy_session' ) );
if ( ! is_user_logged_in() ) { if ( ! is_user_logged_in() ) {
add_action( 'woocommerce_thankyou', array( $this, 'destroy_session' ) ); add_action( 'woocommerce_thankyou', array( $this, 'destroy_session' ) );
} }

View File

@ -32,8 +32,6 @@ add_filter( 'woocommerce_add_to_cart_validation', 'wc_protected_product_add_to_c
/** /**
* Clears the cart session when called * Clears the cart session when called
*
* @return void
*/ */
function wc_empty_cart() { function wc_empty_cart() {
if ( ! isset( WC()->cart ) || WC()->cart == '' ) { if ( ! isset( WC()->cart ) || WC()->cart == '' ) {
@ -43,34 +41,27 @@ function wc_empty_cart() {
} }
/** /**
* Load the cart upon login * Load the persistent cart
* *
* @param string $user_login * @param string $user_login
* @param WP_User $user * @param WP_User $user
* @deprecated 2.3
*/ */
function wc_load_persistent_cart( $user_login, $user ) { function wc_load_persistent_cart( $user_login, $user ) {
if ( ! $user || ! ( $saved_cart = get_user_meta( $user->ID, '_woocommerce_persistent_cart', true ) ) ) {
if ( ! $user ) {
return; return;
} }
$saved_cart = get_user_meta( $user->ID, '_woocommerce_persistent_cart', true ); if ( empty( WC()->session->cart ) || ! is_array( WC()->session->cart ) || sizeof( WC()->session->cart ) === 0 ) {
if ( $saved_cart ) {
if ( empty( WC()->session->cart ) || ! is_array( WC()->session->cart ) || sizeof( WC()->session->cart ) == 0 ) {
WC()->session->cart = $saved_cart['cart']; WC()->session->cart = $saved_cart['cart'];
} }
} }
}
add_action( 'wp_login', 'wc_load_persistent_cart', 1, 2 );
/** /**
* Add to cart messages. * Add to cart messages.
* *
* @access public * @access public
* @param int|array $product_id * @param int|array $product_id
* @return void
*/ */
function wc_add_to_cart_message( $product_id ) { function wc_add_to_cart_message( $product_id ) {

View File

@ -147,6 +147,8 @@ Yes you can! Join in on our [GitHub repository](http://github.com/woothemes/wooc
* Tweak - Use 30 days instead of year for transients to avoid bugs in memcache plugins. * Tweak - Use 30 days instead of year for transients to avoid bugs in memcache plugins.
* Tweak - Add reports menu item if user can access reports but not the main WC section. * Tweak - Add reports menu item if user can access reports but not the main WC section.
* Tweak - Improve grouped product quantity inputs. * Tweak - Improve grouped product quantity inputs.
* Tweak - Load the persistant cart if cart is empty.
* Tweak - Prevent cart being cleared when accessing the login page.
= 2.3.5 - 20/02/2015 = = 2.3.5 - 20/02/2015 =
* Fix - Plain text address formatting. * Fix - Plain text address formatting.