Hi @jonas_op
Yes, there is a way to handle this. You can use a WooCommerce hook to display a custom error message when a customer tries to add a product they’ve already purchased to their cart.
Although writing or providing custom code is not within the scope of our support policy, you can add this custom code snippet to your child theme’s funcitons.php file:
add_filter( 'woocommerce_add_to_cart_validation', 'only_one_in_cart', 99, 2 );
function only_one_in_cart( $passed, $product_id ) {
// If there is a product in the cart
if( WC()->cart->get_cart_contents_count() > 0 ){
// Display a message
wc_add_notice( __( 'You already have a course in your cart. Please complete that purchase before adding another course.', 'woocommerce' ), 'error' );
return false;
}
return $passed;
}
If above code doesn’t work, try this one: https://stackoverflow.com/a/70377722/23495588
If you still need assistance, we recommend asking development questions on the #developers channel of the WooCommerce Community Slack. Many of our developers hang out there and will be able to offer insights into your question. You can also seek help from the following:
I wish I could help more, but hopefully, this gets you going in the right direction to get the job done.