在Woocommerce购物篮中只允许来自同一父类别的商品



我有3个"主"类别,环境,冷藏和冷冻。然后环境又分为3个类别:食品;饮料,非食品和宠物产品,每个都有自己的子类别。我从答案中尝试了以下代码,只允许从WooCommerce中相同父类别的产品添加到购物车中,但它将限制为"非食品",而不是父类别"环境":

add_filter( 'woocommerce_add_to_cart_validation', 'avoid_add_to_cart_from_different_main_categories', 10, 3 );
function avoid_add_to_cart_from_different_main_categories( $passed, $product_id, $quantity ) {
$cart      = WC()->cart;
$taxonomy  = 'product_cat';
$ancestors = [];
if( $cart->is_empty() )
return $passed;
$terms = (array) wp_get_post_terms( $product_id, $taxonomy, array('fields' => 'ids') );
if( count($terms) > 0 ) {
// Loop through product category terms  set for the current product
foreach( $terms as $term) {
foreach( get_ancestors( $term, $taxonomy ) as $term_id ) {
$ancestors[(int) $term_id] = (int) $term_id;
}
}

// Loop through cart items
foreach ( $cart->get_cart() as $item ) {
$terms = (array) wp_get_post_terms( $item['product_id'], $taxonomy, array('fields' => 'ids') );
if( count($terms) > 0 ) {
// Loop through product category terms set for the current cart item
foreach( $terms as $term) {
foreach( get_ancestors( $term, $taxonomy ) as $term_id ) {
$ancestors[(int) $term_id] = (int) $term_id;
}
}
}
}
// When there is more than 1 parent product category
if( count($ancestors) > 1 ) {
wc_add_notice( __('Only products of the same category (Ambient, Chilled or Frozen) can be purchased together.'), 'error' );
$passed = false; 
}
}
return $passed;
}

试试这个。将代码添加到活动主题的functions.php文件中。

add_filter( 'woocommerce_add_to_cart_validation', 'allow_same_parent_category_add_to_cart', 10, 5 );
function allow_same_parent_category_add_to_cart( $passed, $product_id, $quantity, $variation_id = '', $variations = array() ) {
// Get the parent category IDs for the current product
$parent_cat_ids = get_parent_category_ids( $product_id );
// Check if there are items in the cart
if ( ! WC()->cart->is_empty() ) {
// Loop through each item in the cart
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
// Get the parent category IDs for the item in the cart
$cart_item_parent_cat_ids = get_parent_category_ids( $cart_item['product_id'] );
// Check if the parent category IDs intersect
if ( array_intersect( $parent_cat_ids, $cart_item_parent_cat_ids ) ) {
// If the parent category IDs intersect, allow the product to be added to the cart
return $passed;
}
}
// If the parent category IDs don't intersect, show an error message
wc_add_notice( __( 'Only products of the same category (Ambient, Chilled or Frozen) can be purchased together.', 'your-text-domain' ), 'error' );
return false;
} else {
// If the cart is empty, allow the product to be added to the cart
return $passed;
}
}

function get_parent_category_ids( $product_id ) {
$parent_cat_ids = array();
$terms = wp_get_post_terms( $product_id, 'product_cat', array( 'fields' => 'all' ) );
foreach ( $terms as $term ) {
// Check if the term has a parent
if ( $term->parent > 0 ) {
// If the term has a parent, get the parent's ID
$parent_cat_ids[] = $term->parent;
} else {
// If the term has no parent, it is a top-level category and its own ID should be used
$parent_cat_ids[] = $term->term_id;
}
}
return $parent_cat_ids;
}

相关内容

  • 没有找到相关文章

最新更新