在 Woocommerce 购物篮中只允许来自相同父类别的项目

问题描述 投票:0回答:1

我有 3 个“主”类别,常温、冷藏和冷冻。 Ambient 然后分为 3 个其他类别,食品和饮料、非食品和宠物产品,每个类别都有自己的子类别。我在 Only allow add to cart from products of the same parent category in 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;
}
php wordpress woocommerce shopping-cart taxonomy-terms
1个回答
0
投票

试试这个。在活动主题的 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;
}
© www.soinside.com 2019 - 2024. All rights reserved.