Php Woocommerce-防止将两个不同类别的项目添加到购物车

Php Woocommerce-防止将两个不同类别的项目添加到购物车,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,我想防止用户一次将不同类别的产品添加到购物车中。 假设用户导航到另一个类别并尝试将产品添加到购物车中,他们会首先清除购物车。 有人知道我该怎么做吗?我是这方面的新手。 谢谢我以前解决过这个问题 它与您的请求略有不同,因为当他们尝试添加第二个类别时,它不会清除购物车,而是将原始项目保留在购物车中并显示警告 为子孙后代: <?php // Enforce single parent category items in cart at a time based on first item in

我想防止用户一次将不同类别的产品添加到购物车中。 假设用户导航到另一个类别并尝试将产品添加到购物车中,他们会首先清除购物车。 有人知道我该怎么做吗?我是这方面的新手。 谢谢我以前解决过这个问题

它与您的请求略有不同,因为当他们尝试添加第二个类别时,它不会清除购物车,而是将原始项目保留在购物车中并显示警告

为子孙后代:

<?php

// Enforce single parent category items in cart at a time based on first item in cart
function get_product_top_level_category ( $product_id ) {

    $product_terms            =  get_the_terms ( $product_id, 'product_cat' );
    $product_category_term    =  $product_terms[0];
    $product_category_parent  =  $product_terms[0]->parent;

    while ( $product_category_parent  !=  0 ) {
            $product_category_term    =  get_term($product_category_parent, 'product_cat' );
            $product_category_parent  =  $product_category_term->parent;
    }

    return $product_category_term;

}

add_filter ( 'woocommerce_before_cart', 'restrict_cart_to_single_category' );
function restrict_cart_to_single_category() {
        global $woocommerce;
        $cart_contents    =  $woocommerce->cart->get_cart( );
        $cart_item_keys   =  array_keys ( $cart_contents );
        $cart_item_count  =  count ( $cart_item_keys );

        // Do nothing if the cart is empty
        // Do nothing if the cart only has one item
        if ( ! $cart_contents || $cart_item_count == 1 ) {
                return null;
        }

        // Multiple Items in cart
        $first_item                    =  $cart_item_keys[0];
        $first_item_id                 =  $cart_contents[$first_item]['product_id'];
        $first_item_top_category       =  get_product_top_level_category ( $first_item_id );
        $first_item_top_category_term  =  get_term ( $first_item_top_category, 'product_cat' );
        $first_item_top_category_name  =  $first_item_top_category_term->name;

        // Now we check each subsequent items top-level parent category
        foreach ( $cart_item_keys as $key ) {
                if ( $key  ==  $first_item ) {
                        continue;
                }
                else {
                        $product_id            =  $cart_contents[$key]['product_id'];
                        $product_top_category  =  get_product_top_level_category( $product_id );

                        if ( $product_top_category  !=  $first_item_top_category ) {
                                $woocommerce->cart->set_quantity ( $key, 0, true );
                                $mismatched_categories  =  1;
                        }
                }
        }

        // we really only want to display this message once for anyone, including those that have carts already prefilled
        if ( isset ( $mismatched_categories ) ) {
                echo '<p class="woocommerce-error">Only one category allowed in cart at a time.<br />You are currently allowed only <strong>'.$first_item_top_category_name.'</strong> items in your cart.<br />To order a different category empty your cart first.</p>';
        }
}
?>

以下是我的解决方案(您可以用“产品”替换“城市”,使其适用于产品类别:

function validate_add_cart_item( $passed, $product_id, $quantity, $variation_id = '', $variations= '' ) {

    // Get the list of cities for the item to be added
    $product_cities = get_the_terms( $product_id, 'city' );
    $cities_array = array_map(function($city){
        return $city->slug;
    }, $product_cities);

    // Get the cart items
    $item_in_cart = WC()->cart->get_cart();

    // Check for each item if the city is different
    foreach ($item_in_cart as $item){
        $cities = get_the_terms( $item['product_id'], 'city' );
        foreach ($cities as $city){
            if(!in_array($city->slug, $cities_array)){
                $passed = false;
                wc_add_notice( __( 'You cannot mix products from two cities', 'domain' ), 'error' );
            }
        }
    }

    return $passed;
}
add_filter( 'woocommerce_add_to_cart_validation', 'validate_add_cart_item', 10, 5 );

这很好!我确实有一个问题,这是否也可以启用或阻止通过ajax添加到购物车的项目?我已经有一段时间遇到这个问题,当通过ajax添加项目时,它在页面购物车的ajax中显示项目都在。当单击查看购物车时,此方法有效,但当转到“签出”时直接来说,它一次允许两个不同的类别。第一个功能过于复杂,在第一个类别是顶级类别的情况下会失败。我将在适合我的版本中进行编辑。