I have configured WooCommerce composite product's component in the following manner ( see below 2 screenshots ).
On the front-end side of product single page , I am selecting the same product ( eg : Form D ) from those 2 components ( see below screenshot ).
Now , when I click on Add to cart button , it is showing a Notice like : only 1 product may allow adding to cart , but also it is adding the same product to the cart ( see below screenshot ).
Now , to prevent adding same products in the cart , I write following code :
add_filter( 'woocommerce_add_to_cart_validation', 'filter__woocommerce_add_to_cart_validation', 10, 3 );
function filter__woocommerce_add_to_cart_validation ( $passed, $product_id, $quantity ){
$all_pro_id = array();
if ( isset( $_POST['wccp_component_selection'] ) && ! empty( $_POST['wccp_component_selection'] ) ) {
foreach ( $_POST['wccp_component_selection'] as $key => $value ) {
if ( ! empty( $value ) ) {
$all_pro_id[] = $value;
}
}
//echo "<pre>"; print_r($all_pro_id); die;
if ( count( $all_pro_id ) > count( array_unique( $all_pro_id ) ) ) {
//echo "yes";
$passed = false;
}
}
return $passed;
}
Still , the code is not working and it is adding the same product to the cart ( see below screenshot ).




