I have WordPress version 4.9.5 with our own theme, and WooCommerce as online shop solution.
Imagine that some user logs in to the website and adds some items in shop cart. Then he exits from website, whether he closes web page or logs out. After some time (doesn't matter is he on the same computer or not), when same user visit website (as a guest, without login) and adds some items in shoping cart and goes to checkout, WordPress merges two carts (items from the past and currently added in cart). I need to remove old items and keep only new items.
Example: (cart contents when user is logged in)
(cart contents when user is guest/logged out)
(cart contents after logging in during checkout)
I need cart to keep only:
How should I do that?
Combining Sedimu's answer and a piece of code from this link: https://gist.github.com/maxrice/7dc500cd07fa70e2fb5251293d22e485 solution to your problem might be this:
<?php
function clear_persistent_cart_after_login( $user_login, $user ) {
$blog_id = get_current_blog_id();
// persistent carts created in WC 3.1 and below
if ( metadata_exists( 'user', $user->ID, '_woocommerce_persistent_cart' ) ) {
delete_user_meta( $user->ID, '_woocommerce_persistent_cart' );
}
// persistent carts created in WC 3.2+
if ( metadata_exists( 'user', $user->ID, '_woocommerce_persistent_cart_' . $blog_id ) ) {
delete_user_meta( $user->ID, '_woocommerce_persistent_cart_' . $blog_id );
}
}
add_action('wp_login', 'clear_persistent_cart_after_login', 10, 2);
?>
try this:
add_filter( 'woocommerce_persistent_cart_enabled', '__return_false' );
I tested it and it was working for me. I hope it helps you!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With