In certain cases it may make sense to reduce the possible products in the shopping cart to “1”. For example, if you sell a subscription product. You can do that quickly and easily with this WooCommerce snippet.
if (!function_exists( 'evolution_only_one_product_in_cart' ) ) :
/**
* @snippet WooCommerce maximal 1 Produkt im Warenkorb
*/
function evolution_only_one_product_in_cart( $passed, $added_product_id ) {
wc_empty_cart();
return $passed;
}
add_filter( 'woocommerce_add_to_cart_validation', 'evolution_only_one_product_in_cart', 99, 2 );
endif;