1

I have a Woocommerce form to 'Add Funds'. It has an amount input field ($20, $30 ...etc.) and a submit button that redirects to cart page with the input amount as total.

Redirect to checkout is working, but the cart items are not getting removed if a user abandons the cart and tries to order again.

I tried numerous solutions for the redirect to checkout, but only one worked.

Working solution for redirect to checkout:

WooCommerce - Skip cart page redirecting to checkout page

Solutions not working for redirect to checkout:

https://wordpress.stackexchange.com/questions/267071/redirect-to-woocommerce-checkout-after-adding-to-cart-item-already-in-cart

Woocommerce add to cart button redirect to checkout

N.B. I have added the working and not working solutions for redirect to checkout because it may provide an insight as to why the empty cart solutions are not working.

Incase of emptying cart before adding a new product, none of the solutions are working:

https://gist.github.com/viniciusrtf/b49403b5f87dcd7699c1

https://hungred.com/how-to/empty-woocommerce-cart-adding-item/

https://wordpress.stackexchange.com/questions/267071/redirect-to-woocommerce-checkout-after-adding-to-cart-item-already-in-cart

Using Woocommerce 3.2.6 and WordPress 4.9.2

mesumosu
  • 155
  • 2
  • 3
  • 15

1 Answers1

4

First you will need in WooCommerce Settings > Products > Display in "Add to cart behaviour" to enabled the checkbox : Redirect to the cart page after successful addition

Then you will need the 3 following hooked function:

1) Empty cart before add-to-cart (if cart is not empty)

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

2) Add-to-cart redirection to checkout:

add_filter( 'woocommerce_add_to_cart_redirect', 'add_to_cart_checkout_redirection', 10, 1 );
function add_to_cart_checkout_redirection( $url ) {
    return wc_get_checkout_url();
}

3) Skip cart page redirecting to checkout:

add_action('template_redirect', 'skip_cart_page_redirection_to_checkout');
function skip_cart_page_redirection_to_checkout() {
    if( is_cart() )
        wp_redirect( wc_get_checkout_url() );
}

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

Tested and works (with Woocommerce 3.2.6 and WordPress 4.9.2 on Storefront theme).

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • The cart is not getting emptied before add new items, and giving the error 'You cannot add another "Product" to your cart'. I was wondering if we can use the clear cart session function. I am a novice with php. `function wc_empty_cart() { if ( ! isset( WC()->cart ) || '' === WC()->cart ) { WC()->cart = new WC_Cart(); } WC()->cart->empty_cart( false ); }` – mesumosu Jan 29 '18 at 23:13
  • 1
    @mesumosu I don't get any error. I have tested the code in all possible ways. So you should try to remove before, other related customization code. This issue can also come from your theme or maybe from a plugin. – LoicTheAztec Jan 29 '18 at 23:32
  • 1
    Thank u very much!!! You saved me hours of frustation by pointing me out to the plugin angle. I was using a free Woocommerce wallet plugin that was reassigning the `$product_id` to something different. Used the new assigned variable in place of `$product_id` and your solution for empty cart ( Code 1) worked. So, its been a lesson to learn, that when using woocommerce plugins, we have to check for the variables `$product_id` and `$cart_item_key` in the plugin. Well, what can I say, you are an amazing support!!!! – mesumosu Jan 30 '18 at 00:48
  • thank you. doesnt work on woocommerce 4+ can you update codes for woocomerce 4 and wordpress 5? – ahmadwp Jul 18 '20 at 11:27