Php Woo商务购物车验证类别

Php Woo商务购物车验证类别,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,我在我的Wordpress/Woocommerce网站上测试了这段代码 add_filter( 'woocommerce_add_to_cart_validation', 'add_to_cart_validation_callback', 10, 3 ); function add_to_cart_validation_callback( $passed, $product_id, $quantity) { // HERE set your alert text message $messag

我在我的Wordpress/Woocommerce网站上测试了这段代码

add_filter( 'woocommerce_add_to_cart_validation', 'add_to_cart_validation_callback', 10, 3 );
function add_to_cart_validation_callback( $passed, $product_id, $quantity) {
// HERE set your alert text message
$message = __( 'ohoh dit gaat niet', 'woocommerce' );

if( ! WC()->cart->is_empty() ) {
    // Get the product category terms for the current product
    $terms_slugs = wp_get_post_terms( $product_id, 'product_cat', array('fields' => 'slugs'));

    // Loop through cart items
    foreach (WC()->cart->get_cart() as $cart_item ){
        if(  has_term( $terms_slugs, 'product_cat', $cart_item['product_id'] )) {
            $passed = false;
            wc_add_notice( $message, 'error' );
            break;
        }
    }
}
return $passed;
}
使用此代码,您只能从一个类别中购买一些东西。我在找其他类似的东西

我有4个类别 -家 -花园 -装备 -玩具

如果用户将玩具中的产品放入购物车,则无法从其他类别中购买

但是,如果用户把一个产品从花园里放进购物车,他可以把它与家庭和装备分类结合起来


概述:用户可以将产品与家居、花园和服装结合起来,但不能与玩具结合起来。

检查此项,您迄今为止尝试过什么?