21

How to Limit WooCommerce Cart to One Product and Redirect to Checkout

Powered by WPCodeBox

Optimize your WooCommerce store for single item sales. This snippet limits the cart to one product, automatically removes existing items, adds the new product, and redirects directly to checkout. Perfect for pricing tables or single-item purchases, it also removes the “order again” button for a streamlined user experience.


<?php 

add_filter( 'woocommerce_add_to_cart_redirect', function() {
   return wc_get_checkout_url();
});

add_filter( 'wc_add_to_cart_message_html' , function() {
   return '';
});
 

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

    return $passed;
}
, 20, 3 );

remove_action( 'woocommerce_order_details_after_order_table', 'woocommerce_order_again_button' );

Other Snippets

WPCodeBox is a WordPress Code Snippets Manager that allows you to share your WordPress Code Snippets across your sites.