簡體   English   中英

在 Woocommerce 中一次只允許購物車中的一個產品類別

[英]Allow only one product category in cart at once in Woocommerce

我將如何 go 關於配置 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) {

    // Getting the product categories term slugs in an array for the current product
    $term_slugs   = wp_get_post_terms( $product_id, 'product_cat', array('fields' => 'slugs') );

    // Loop through cart items
    foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item ){

        // Check if the product category of the current product don't match with a cart item
        if( ! has_term( $term_slugs, 'product_cat', $cart_item['product_id'] ) ){

            // Displaying a custom notice
            wc_add_notice( __('Only items from one product category are allowed in cart'), 'error' );

            // Avoid add to cart
            return false; // exit
        }
    }
    return $passed;
}

代碼位於活動子主題(或活動主題)的 function.php 文件中。 測試和工作。


添加(更新) - 相同但僅適用於父產品類別

add_filter( 'woocommerce_add_to_cart_validation', 'only_one_product_category_allowed', 20, 3 );
function only_one_product_category_allowed( $passed, $product_id, $quantity) {
    $parent_term_ids = $item_parent_term_ids = array(); // Initializing

    // Loop through the current product category terms to get only parent main category term
    foreach( get_the_terms( $product_id, 'product_cat' ) as $term ){
        if( $term->parent > 0 ){
            $parent_term_ids[] = $term->parent; // Set the parent product category
        }
    }

    // Loop through cart items
    foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item ){
        // Loop through the cart item product category terms to get only parent main category term
        foreach( get_the_terms( $cart_item['product_id'], 'product_cat' ) as $term ){
            if( $term->parent > 0 ){
                $item_parent_term_ids[] = $term->parent; // Set the parent product category
            }
        }

        // Check if parent product categories don't match
        if( ! array_intersect( $parent_term_ids, $item_parent_term_ids ) ){

            // Displaying a custom notice
            wc_add_notice( __('Only items from one product category are allowed in cart'), 'error' );

            // Avoid add to cart
            return false; // exit
        }
    }
    return $passed;
}

代碼位於活動子主題(或活動主題)的 function.php 文件中。 測試和工作。

@LoicTheAztec 如果我只想允許添加某個類別中的產品以在結帳時添加,該怎么辦。 我的情況是,我有一些可預訂的產品和實物產品,它們都有不同的運費。 所以我想要一個案例,如果有人訂購可預訂產品,他們將無法將任何實物產品添加到購物車,除非他們在單獨的訂單上這樣做。 謝謝

感謝LoicTheAztec提供了很好的答案!
但是,父產品類別版本不適合我的情況。
LoicTheAztec的版本在產品的類別沒有父類別時返回 false,即使這些產品具有相同的類別。
這是處理上述情況的修改。

add_filter( 'woocommerce_add_to_cart_validation', 'only_one_product_category_allowed', 20, 3 );
function only_one_product_category_allowed( $passed, $product_id, $quantity) {
    $parent_term_ids = $item_parent_term_ids = array(); // Initializing

    // Loop through the current product category terms to get the parent main category term if any.
    // Otherwise we get the current product's own category.
    foreach( get_the_terms( $product_id, 'product_cat' ) as $term ){
        if ( $term->parent > 0 ){
            $parent_term_ids[] = $term->parent; // Set the parent product category
        } else {
            $parent_term_ids[] = $term->term_id; // Set the category itself of the current product for later comparison
        }
    }

    // Loop through cart items
    foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item ){
        // Loop through the cart item product category terms to get the parent main category term if any.
        // Otherwise we get the cart item's own category.
        foreach( get_the_terms( $cart_item['product_id'], 'product_cat' ) as $term ){
            if ( $term->parent > 0 ){
                $item_parent_term_ids[] = $term->parent; // Set the parent product category
            } else {
                $item_parent_term_ids[] = $term->term_id; // Set the category itself of the cart item for later comparison
            }
        }

        // Check if parent product categories don't match
        if( ! array_intersect( $parent_term_ids, $item_parent_term_ids ) ){

            // Displaying a custom notice
            wc_add_notice( __('Only items from one product category are allowed in cart'), 'error' );

            // Avoid add to cart
            return false; // exit
        }
    }
    return $passed;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM