My intention is that free and paid products cannot be bought together in WooCommerce.
If there is a free product or a zero price product in the basket, all paid products should be removed from the basket OR the opposite situation.
In short, paid and free products cannot be ordered at the same time.
The code below is an approximation of what I want to work towards, but it doesn't fully answer my question. How should I handle this?
add_action( 'template_redirect', 'test_cart_values' );
function test_cart_values(){
$ic_cart = false;
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$product = $cart_item['data'];
$price = WC()->cart->get_product_price( $product );
if ( $price == 0 || $price === '') {
$ic_cart = true;
break;
}
}
if ( $ic_cart ) {
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$product = $cart_item['data'];
$price = WC()->cart->get_product_price( $product );
if ( $price != 0 || $price !== '') {
WC()->cart->remove_cart_item( $cart_item_key );
break;
}
}
}
}