Here is the code that prevent to have more than one downloadable product in the cart:
add_filter( 'woocommerce_add_to_cart_validation', 'only_one_product_type_allowed', 10, 3 );
function only_one_product_type_allowed( $passed, $product_id, $quantity ) {
$product = wc_get_product( $product_id );
$product_type = $product->get_type();
$product_downloadable = $product->is_downloadable();
foreach ( WC()->cart->get_cart() as $cart_item ) {
if ( $product_type == $cart_item['data']->get_type() && $product_downloadable == $cart_item['data']->is_downloadable() && ! $product->is_virtual() ) {
wc_add_notice( __( "NOTICE ON CART PAGE.", "woocommerce" ), 'error' );
return false;
}
}
return $passed;
}
Now , it is not allowing more than one downloadable products in single cart which is OK. But it is not allowing more than one regular items in cart too. it is throwing same notice. can anyone help with fix.
To allow only one downloadable product in cart, allowing other products kind, try to use the following instead:
Code goes in functions.php file of your child theme (or in a plugin). It should work.