Hi there @saurabhkamyab
First of all, please accept my apologies it took us so long to reply to your request! I see that we have replied to threads that were opened after this one, so it looks like this thread fell through the cracks ??
By default, there is an ‘or’ relationship between the terms you add in the Product/Category Dependencies field. This means that even if one product that belongs in one of the selected Categories is in the cart, customers will be able to purchase the current product.
As far as I understand, you’d like to create a more sophisticated rule, where customers will be allowed to purchase this product if they have purchased one product from each of the Categories you have selected — this would mean having an ‘and’ relationship between the Categories you have selected. Is this correct?
If so, then please use the following snippet:
add_filter( 'wc_pd_dependency_relationship', 'sw_pd_force_and_relationship', 10, 2 );
function sw_pd_force_and_relationship( $relationship, $product ) {
if ( in_array( $product->get_id(), array( 123, 456, ) ) ) {
$relationship = 'and';
}
return $relationship;
}
Before using this snippet, please replace 123 and 456 with the IDs of the products to which you’d like it to apply (from the ones that have Product Dependencies rules).
If you are not certain how to use snippets like this, I’d suggest having a look at this plugin — you may find it quite useful as it allows you to manage all your snippets in one place.
Let me know if this helps! Again, I’m very sorry for taking so long to reply to you!
Cheers,
Maria Chekimoglou
Support Team Lead | SomewhereWarm | https://somewherewarm.com/