Reputation: 23
I have two product types, one of them simple product $cart_item['default-engraving']
and the other one credit product $cart_item['default-engraving'] && $cart_item['iconic-engraving']
. I'm trying to find a solution how to make that if I add simple product to a cart, credit product shouldn't be added. Or if I add credit product to a cart, simple product should't be added. But if I want I could add same type for example simple product type as many as I want.
Upvotes: 2
Views: 2342
Reputation: 76
please check below code.
<?php
function ji_woocommerce_add_to_cart_validation( $valid, $product_id, $quantity )
{
$_product = wc_get_product( $product_id);
foreach (WC()->cart->get_cart() as $item ){
$checkTypes[] = wc_get_product( $item['product_id'] )->get_type();
}
$checkTypes = array_unique( $types );
if(count($checkTypes) > 0)
{
if(!in_array($_product->get_type(),$checkTypes))
{
wc_add_notice( __('Here you can add your error message','textdomain'), 'error' );
return false;
}
}
return $valid;
}
add_filter( 'woocommerce_add_to_cart_validation', 'ji_woocommerce_add_to_cart_validation', 10, 3 );
?>
Upvotes: 0
Reputation: 253784
Update: Is not possible to detect custom cart item data on add to cart event.
Checking cart items will allow you to prevent having cart items that have $cart_item['default-engraving']
and $cart_item['iconic-engraving']
at the same time:
add_action( 'woocommerce_check_cart_items', 'check_cart_items_custom_data' );
function check_cart_items_custom_data() {
// Initializing: set the current product type in an array
$types = [];
// Loop through cart items
foreach (WC()->cart->get_cart() as $item ){
if( isset( $item['default-engraving'] ) )
$types[] = 'default';
if( isset( $item['iconic-engraving'] ) )
$types[] = 'iconic';
}
$types = array_unique( $types );
// Check the number of product types allowing only one
if( count( $types ) > 1 ){
// Displaying a custom notice and avoid checkout
wc_add_notice( __('Only items from one product type are allowed in cart'), 'error' );
}
}
Code goes in functions.php file of the active child theme (or active theme). Tested and works.
Original answer: (it can't work for your case as it's not possible to detect that on add to cart event)
Here is the way targeting the product type to allow only one in cart:
add_filter( 'woocommerce_add_to_cart_validation', 'only_one_product_type_allowed', 10, 3 );
function only_one_product_type_allowed( $passed, $product_id, $quantity ) {
// Initializing: set the current product type in an array
$types = [ wc_get_product( $product_id )->get_type() ];
// Loop through cart items
foreach (WC()->cart->get_cart() as $item ){
// Set each product type in the array
$types[] = wc_get_product( $item['product_id'] )->get_type();
}
$types = array_unique( $types );
// Check the number of product types allowing only one
if( count( $types ) > 1 ){
// Displaying a custom notice
wc_add_notice( __('Only items from one product type are allowed in cart'), 'error' );
return false; // Avoid add to cart
}
return $passed;
}
Code goes in functions.php file of the active child theme (or active theme). Tested and works.
Related: Allow only one product category in cart at once in Woocommerce
Upvotes: 4
Reputation: 488
You may try this
add_filter( 'woocommerce_add_to_cart_validation', 'only_one_product_category_allowed', 20, 3 );
function only_one_product_category_allowed( $passed, $product_id, $quantity) {
// Getting the product categories term slugs in an array for the current product
$term_slugs = wp_get_post_terms( $product_id, 'product_cat', array('fields' => 'slugs') );
// Loop through cart items
foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item ){
// Check if the product category of the current product don't match with a cart item
$_product = wc_get_product( $cart_item['product_id']; );
if( $_product->is_type( 'simple' ) ) {
// code to not allow other product type
} else {
// do stuff for everything else
}
}
}
Upvotes: 0