12

I am using WP Job manager with Woo Subscriptions.

Now:

  1. Initially, I selected a package(Woo Subscription)
  2. Then I added all the details.
  3. But did not submit it.
  4. Came back to the site, so to buy again I need to select a package. So I selected the package and filled in details and went to the payment package.
  5. Now in my cart both the packages are present (i.e the one I selected without buying first time and the recent one)
  6. How can this be fixed so the latest selected one is in the cart and earlier one deleted as soon as latest one selected.

I tried this Woocommerce Delete all products from cart and add current product to cart but did not help.

Maxime
  • 8,645
  • 5
  • 50
  • 53
Ram
  • 319
  • 1
  • 2
  • 16

2 Answers2

32

Updated (with 2 different alternatives):

You should try the following:

add_filter( 'woocommerce_add_to_cart_validation', 'remove_cart_item_before_add_to_cart', 20, 3 );
function remove_cart_item_before_add_to_cart( $passed, $product_id, $quantity ) {
    if( ! WC()->cart->is_empty() )
        WC()->cart->empty_cart();
    return $passed;
}

Code goes in functions.php file of your active child theme (or theme). Tested and works with both ajax-add-to-cart and normal add-to-cart…


Or you can use this different one that will keep in cart the last added item:

// Keep only last cart item
add_action( 'woocommerce_before_calculate_totals', 'keep_only_last_cart_item', 30, 1 );
function keep_only_last_cart_item( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;

    $cart_items = $cart->get_cart();

    if( count( $cart_items ) > 1 ){
        $cart_item_keys = array_keys( $cart_items );
        $cart->remove_cart_item( reset($cart_item_keys) );
    }
}

Code goes in function.php file of your active child theme (or theme). Tested and works

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Thanks for your quick response buddy, I added this filter i functions.php, it did not work. The package gets selected using "Ajax" I am not sure if that's causing the problem. -Select Package-->AddPosting->CheckOut page is the flow. – Ram Mar 20 '18 at 11:20
  • @Ram This code is tested and perfectly works with add to cart ajax and normal add to cart. The hook that I use is located either in normal `WC_Cart` `add_to_cart()` method and in `WC_Ajax` `add_to_cart()` method too… Is not possible for anybody to reproduce your issue, your settings, your installation and plugins… – LoicTheAztec Mar 20 '18 at 11:41
  • @Ram I have added another different hooked function that could work for your case… Please try it and let me know if it works… Thanks. – LoicTheAztec Mar 20 '18 at 13:06
  • I am unsure why first one did not work. But the second filter you shared worked like a charm. Thanks LoicTheAztec – Ram Mar 20 '18 at 17:22
  • @ LoicTheAztec I have already up voted the answer, thanks again. – Ram Mar 21 '18 at 10:47
-1

Go to your site database and look for table 'wp_woocommerce_sessions' and see if its 'session_id' column contains 0 value if so

  1. delete all these 0 entry records
  2. make 'session_id' Column as primary.
  3. make 'session_id' Column auto increment.
Waiseman
  • 31
  • 3