Get the solution ↓↓↓
There is a much simpler way using Add to cart validation hook this way:
add_filter( 'woocommerce_add_to_cart_validation', 'wc_add_to_cart_validation_filter_callback', 10, 3 );
function wc_add_to_cart_validation_filter_callback( $passed, $product_id, $quantity ) {
if( WC()->cart->is_empty() )
return $passed;
$product_id1 = 37; // Single product Id
$product_id2 = 19; // Variable product Id
// Loop through cart items
foreach( WC()->cart->get_cart() as $cart_item ) {
if( ( $product_id = $product_id1 && $cart_item['product_id'] == $product_id2 )
|| ( $product_id = $product_id2 && $cart_item['product_id'] == $product_id1 ) ) {
wc_add_notice( sprintf(
__("This product can't not be purchased toggether with %s."),
'"<strong>' . $cart_item['data']->get_name() . '</strong>"'
), 'error' );
return false;
}
}
return $passed;
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
Addition (related to your comment):
Another way is to remove the first item silently when both products are in cart:
add_action('woocommerce_before_calculate_totals', 'keep_last_variation_from_variable_products', 10, 1 );
function keep_last_variation_from_variable_products( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$product_id1 = 37; // Single product Id
$product_id2 = 19; // Variable product Id
$product_ids = array($product_id1, $product_id2);
$items_keys = array();
// Loop through cart items
foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
// If cart item matches with one of our defined product
if ( in_array( $cart_item['product_id'], $product_ids ) ) {
// Set the cart item key in an array (avoiding duplicates)
$items_keys[$cart_item['product_id']] = $cart_item_key;
}
}
// If both products are in cart just keep the last one silently
if( count($items_keys) > 1 ) {
$cart->remove_cart_item( reset($items_keys) ); // remove the first one
}
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.