WooCommerce If product is already in a cart
<?php
add_filter( 'woocommerce_add_cart_item_data', 'woo_custom_add_to_cart' , 10, 3);
function woo_custom_add_to_cart( $cart_item_data, $product_id, $variation_id ) {
foreach( WC()->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
if( $product_id == $_product->id ) {
WC()->cart->set_quantity($cart_item_key, 0);
}
}
// Do nothing with the data and return
return $cart_item_data;
}