-1

I am trying to remove the full cart feature from WooCommerce. I want when some one click on the buy now button they should go directly to the checkout page.

** I have tried few plugins but those don't remove the full cart feature. They just redirect to the checkout page. ** So with the plugins my problem was if someone leave from the checkout page and try to buy the same product or an another product the previous product was in the cart. and in the checkout page there was 2 products.

Can someone tell me how can i remove the full cart feature from the woo-commerce? Is it possible?

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399

1 Answers1

1

So if you want to always have only one item in your cart you add this to your functions.php

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;
}

It will empty your cart before adding the new item to it. And you can keep your plugin that is redirecting you to checkout or code it in php probably with something like that :

add_action( 'template_redirect', 'redirect_cart_to_checkout' );
function redirect_cart_to_checkout() {
    if( is_cart()){
        wp_redirect(wc_get_checkout_url());
        die;
    }
}
Alexis Vandepitte
  • 2,077
  • 2
  • 12
  • 28