WooCommerce按产品类别限制购物车商品

时间:2020-01-23 17:24:08

标签: php wordpress woocommerce categories cart

我正在添加代码,以限制购物车不能将其他产品添加到产品的专有类别(另一个类别除外),但是由于某种原因,我的代码只能两次执行,并且在不修改任何内容的情况下将无法使用。这真让我感到困惑。

我正在运行WordPress 5.3.2和WooCommerce 3.6.5。

代码基于this thread,并具有类似的限制购物车商品的情况。

请在下面查看我的代码:

add_filter('woocommerce_add_to_cart_validation', 'check_breakfast_items', 10, 3);
function check_breakfast_items( $true, $product_id, $quantity ) {
    // Set warning notice
    $notice = __( 'Breakfast and non-breakfast items cannot be ordered at the same time.', 'woocommerce' );

    $category = 'breakfast';        // Set exclusive category
    $exempt_category = 'extras';    // Set exempted category not to be restricted
    $true = true;                   // Initializing
    $breakfast_product = false;
    $breakfast_in_cart = false;
    $extras_product = false;
    $extras_in_cart = false;

    // Check if cart has items
    if (!WC()->cart->is_empty() ) {
        $terms_slugs = wp_get_post_terms( $product_id, 'product_cat', array('fields' => 'slugs') );
        // Check if the product has breakfast category
        if ( in_array( $category, $terms_slugs ) ) {
            $breakfast_product = true;
        } else {
            $breakfast_product = false;
        }
        // Check if the product has extras category
        if ( in_array( $exempt_category, $terms_slugs ) ) {
            $extras_product = true;
        } else {
            $extras_product = false;
        }

        // Check each cart item
        foreach( WC()->cart->get_cart() as $cart_item ) {
            $cart_item_id = $cart_item['product_id'];
            $cart_terms_slugs = wp_get_post_terms( $cart_item_id, 'product_cat', array('fields' => 'slugs') );
            // Check if any cart items have breakfast category
            if ( in_array( $category, $cart_terms_slugs ) ) {
                $breakfast_in_cart = true;
            } else {
                $breakfast_in_cart = false;
            }
            // Check if any cart items have extras category
            if ( in_array( $exempt_category, $cart_terms_slugs ) ) {
                $extras_in_cart = true;
            } else {
                $extras_in_cart = false;
            }
        }

        // Check cart items for restriction
        if ( $breakfast_product == $breakfast_in_cart ){
            $true = true; // Restriction passed if product and cart items are all breakfast
        } else if ( $breakfast_product == $extras_in_cart || $extras_product == $breakfast_in_cart ) {
            $true = true; // Restriction passed if product and/or cart items are breakfast and extras
        } else if ( $breakfast_product != $breakfast_in_cart ) {
            $true = false; // Restriction not passed if product and cart items are breakfast and non-breakfast
            wc_add_notice( $message, 'error' );
        }
    }
    return $true;
}

原始代码使用has_term搜索产品类别标签,但由于某些原因,这对我不起作用(似乎无法在多个类别的产品上使用它?)

无法弄清楚为什么在没有任何更改的情况下代码会停止工作。如果有人能对此有所了解,那就太好了。谢谢!

0 个答案:

没有答案