Woocommerce购物车类别限制

时间:2020-05-20 01:20:16

标签: php wordpress woocommerce

我正在使用以下代码限制购物车中某一类别的产品。

add_filter( 'woocommerce_add_to_cart_validation', 'only_one_product_category_allowed', 20, 3 );
function only_one_product_category_allowed( $passed, $product_id, $quantity) {

    $term_slugs   = wp_get_post_terms( $product_id, 'product_cat', array('fields' => 'slugs') );

    foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item ){

        if( ! has_term( $term_slugs, 'product_cat', $cart_item['product_id'] ) ){

            wc_add_notice( __('Only goods from ONE category allowed in cart'), 'error' );
            echo "<script type='text/javascript'>alert('Only goods from ONE category allowed in cart');</script>";

            return false;
        }
    }
    return $passed;
}

有人可以让我知道如何更改购物车中允许的类别数量,而不是一个吗?我想允许购物车中包含三类物品。

感谢您的帮助!

欢呼

0 个答案:

没有答案