Forum Replies Created

Viewing 3 replies - 1 through 3 (of 3 total)
  • Joan

    (@jllorca)

    @bzadmin Your solution works partially, because if I do a second “Switch to” for the same user, the cart is deleted.

    Hi @johnbillion

    Steps to reproduce the issue

    • In an incognito window, log in with a customer role account, browse the shop, add items to the cart, and then log out.
    • Check the session_key record for the same customer (user_id) in the wp_woocommerce_sessions table. Inside the session_value, you will see the data structure containing the items you added to the cart.
    • Now log in with your administrator account and use the “Switch To” function to switch into the same customer account.
    • At this point, the cart appears empty. If you check the session_value again, you’ll notice that the cart item data has disappeared.

    If you don’t use “Switch To” and simply log in as the customer directly, the cart data is preserved (until the session naturally expires).

    Hi, I’m also experiencing the same issue described by Denis.

    If you still have the cart data available in the persistent cart, you can use the following snippet to restore it and move the lines back into the session cart:

    add_filter('woocommerce_persistent_cart_enabled', '__return_true');

    add_action('woocommerce_cart_loaded_from_session', function($cart) {
    if ( ! is_user_logged_in() || ! function_exists('WC') ) {
    return;
    }

    $user_id = get_current_user_id();

    // If the session cart already has items, do nothing.
    if ( ! $cart->is_empty() ) {
    return;
    }

    $key = '_woocommerce_persistent_cart_' . get_current_blog_id();
    $saved = get_user_meta($user_id, $key, true);

    if ( empty($saved) || empty($saved['cart']) || ! is_array($saved['cart']) ) {
    return;
    }

    WC()->cart->empty_cart(true);

    foreach ( $saved['cart'] as $item ) {
    $product_id = isset($item['product_id']) ? (int) $item['product_id'] : 0;
    $variation_id = isset($item['variation_id']) ? (int) $item['variation_id'] : 0;
    $quantity = isset($item['quantity']) ? (int) $item['quantity'] : 1;
    $variation = isset($item['variation']) ? (array) $item['variation'] : [];

    $cart_item_data = $item;
    unset($cart_item_data['product_id'], $cart_item_data['variation_id'], $cart_item_data['quantity'], $cart_item_data['variation'], $cart_item_data['data_hash']);

    if ( $product_id > 0 && $quantity > 0 ) {
    WC()->cart->add_to_cart($product_id, $quantity, $variation_id, $variation, $cart_item_data);
    }
    }

    WC()->cart->calculate_totals();
    }, 20);

    After adding the snippet, you need to switch to the user account that lost the cart, and the cart data should be restored.

    Once you’ve recovered the data, I recommend removing this snippet and also disabling the “Switch To” plugin until a proper fix for this issue is released — or alternatively, avoid switching to another user in the meantime.

    For reference, here’s a link to the latest changes related to cart handling on GitHub:

    https://github.com/woocommerce/woocommerce/pull/57961

    • This reply was modified 10 months, 1 week ago by Joan.
Viewing 3 replies - 1 through 3 (of 3 total)