I'm creating a course site where it is possible to register and pay via Woocomerce, I need to limit the possibility of adding multiple courses (products) to the cart but leave the possibility to select the quantity. I found several indications on how not to allow the insertion of multiple products in the cart including the quantity but not as I want. therefore you should be able to choose only one product but with the desired quantity.
I solved with this code
add_filter( 'woocommerce_add_to_cart_validation', 'add_to_cart_validation', 10, 1 );
function add_to_cart_validation( $passed ) {
if( ! WC()->cart->is_empty() ){
wc_add_notice( __(“Orders are limited to one item at a time”, "woocommerce" ), 'error' );
$passed = false;
}
return $passed;
}```