Limit 1 product per checkout in WooCommerce

Code below can be added to your theme’s functions.php. What it does is remove products from cart everytime the user opens another product. This can be useful for Businesses that sells plans and similar products.

// Only 1 item per Checkout
function only_one_in_cart( $passed, $added_product_id ) {
   wc_empty_cart();
   return $passed;
}
add_filter('woocommerce_add_to_cart_validation', 'only_one_in_cart', 99, 2 );