From ccb54faf0b2073addeaba36493aed6efab380e8b Mon Sep 17 00:00:00 2001 From: Gerhard Potgieter Date: Tue, 5 Dec 2017 08:45:52 +0200 Subject: [PATCH 1/2] When getting a cart from a session and there are contents as well as contents in the persistant cart, merge the two carts to avoid loosing the persistant cart content. --- includes/class-wc-cart-session.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/includes/class-wc-cart-session.php b/includes/class-wc-cart-session.php index 1b905ff8da4..70e40c8ad72 100644 --- a/includes/class-wc-cart-session.php +++ b/includes/class-wc-cart-session.php @@ -76,6 +76,9 @@ final class WC_Cart_Session { $update_cart_session = true; } elseif ( is_null( $cart ) ) { $cart = array(); + } elseif ( is_array( $cart ) && ( $saved_cart = get_user_meta( get_current_user_id(), '_woocommerce_persistent_cart_' . get_current_blog_id(), true ) ) ) { + $cart = array_merge( $saved_cart['cart'], $cart ); + $update_cart_session = true; } if ( is_array( $cart ) ) { From 961a70965d30e110f3c691b4f282f5f4cd80b4b8 Mon Sep 17 00:00:00 2001 From: Gerhard Potgieter Date: Tue, 5 Dec 2017 08:48:35 +0200 Subject: [PATCH 2/2] PHPCS Fixes --- includes/class-wc-cart-session.php | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/includes/class-wc-cart-session.php b/includes/class-wc-cart-session.php index 70e40c8ad72..0f555d6ecd6 100644 --- a/includes/class-wc-cart-session.php +++ b/includes/class-wc-cart-session.php @@ -71,12 +71,12 @@ final class WC_Cart_Session { $this->cart->set_coupon_discount_tax_totals( WC()->session->get( 'coupon_discount_tax_totals', array() ) ); $this->cart->set_removed_cart_contents( WC()->session->get( 'removed_cart_contents', array() ) ); - if ( is_null( $cart ) && ( $saved_cart = get_user_meta( get_current_user_id(), '_woocommerce_persistent_cart_' . get_current_blog_id(), true ) ) ) { + if ( is_null( $cart ) && ( $saved_cart = get_user_meta( get_current_user_id(), '_woocommerce_persistent_cart_' . get_current_blog_id(), true ) ) ) { // @codingStandardsIgnoreLine $cart = $saved_cart['cart']; $update_cart_session = true; } elseif ( is_null( $cart ) ) { $cart = array(); - } elseif ( is_array( $cart ) && ( $saved_cart = get_user_meta( get_current_user_id(), '_woocommerce_persistent_cart_' . get_current_blog_id(), true ) ) ) { + } elseif ( is_array( $cart ) && ( $saved_cart = get_user_meta( get_current_user_id(), '_woocommerce_persistent_cart_' . get_current_blog_id(), true ) ) ) { // @codingStandardsIgnoreLine $cart = array_merge( $saved_cart['cart'], $cart ); $update_cart_session = true; } @@ -100,7 +100,9 @@ final class WC_Cart_Session { } else { // Put session data into array. Run through filter so other plugins can load their own session data. - $session_data = array_merge( $values, array( 'data' => $product ) ); + $session_data = array_merge( $values, array( + 'data' => $product, + ) ); $cart_contents[ $key ] = apply_filters( 'woocommerce_get_cart_item_from_session', $session_data, $values, $key ); // Add to cart right away so the product is visible in woocommerce_get_cart_item_from_session hook. @@ -183,7 +185,9 @@ final class WC_Cart_Session { */ public function persistent_cart_update() { if ( get_current_user_id() ) { - update_user_meta( get_current_user_id(), '_woocommerce_persistent_cart_' . get_current_blog_id(), array( 'cart' => $this->get_cart_for_session() ) ); + update_user_meta( get_current_user_id(), '_woocommerce_persistent_cart_' . get_current_blog_id(), array( + 'cart' => $this->get_cart_for_session(), + ) ); } }