指定特定分類的商品無法混購 加入商品時刪除購物車內商品(只能購買1件)

參考連結:Disable shopping when an item from a specific product category is in cart in Woocommerce

參考連結:WooCommerce – Prevent Product Being Added To Cart If A Specific Product Is Already In Cart

參考連結:Prevent add to cart if cart contains a specific Woocommerce product category

//當特定的產品添加到購物車時刪除其他項目 並且只能購買此特定商品 無法與其他商品混購 同時也只能購買一件

// Remove other items when our specific product is added to cart
add_action( 'woocommerce_add_to_cart', 'remove_other_products_on_add_to_cart', 10, 6 );
function remove_other_products_on_add_to_cart ( $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data ){
    // HERE set your product category (can be term IDs, slugs or names)
    $category = '商品分類';

    // We remove other items when our specific product is added to cart
    if( has_term( $category, 'product_cat', $product_id ) ) {
        foreach( WC()->cart->get_cart() as $item_key => $cart_item ){
            if( ! has_term( $category, 'product_cat', $cart_item['product_id'] ) ) {
                WC()->cart->remove_cart_item( $item_key );
            }
        }
    }
}

// Avoid other items to be added to cart when our specific product is in cart
add_filter( 'woocommerce_add_to_cart_validation', 'check_and_limit_cart_items', 10, 3 );
function check_and_limit_cart_items ( $passed, $product_id, $quantity ){
    // HERE set your product category (can be term IDs, slugs or names)
    $category = '商品分類';

    // We exit if the cart is empty
    if( WC()->cart->is_empty() )
        return $passed;

    // CHECK CART ITEMS: search for items from product category
    foreach ( WC()->cart->get_cart() as $cart_item ){
        if( has_term( $category, 'product_cat', $cart_item['product_id'] ) ) {
            // Display an warning message
            wc_add_notice( __('限定商品已於購物車中 (無法與其他商品混購)', 'woocommerce' ), 'error' );
            // Avoid add to cart
            return false;
        }
    }
    return $passed;
}

指定特定分類的商品無法混購 同分類商品可多件購買

//*** 防止特定商品分類與其他商品混購 更改XXX ***//
function dont_add_gas_to_cart_containing_other($validation, $product_id) {

// 將標誌設置為false,直到我們在特定分類中找到產品
    $cart_has_gas = false;

// 如果購物車商品在特定分類中,則設置 $cat_check true
    foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) {

        $product = $cart_item['data'];

        if (has_term('XXX', 'product_cat', $product->id)) {
            $cart_has_gas = true;
            // break because we only need one "true" to matter here
            break;
        }
    }

    $product_is_gas = false;
    if (has_term('mug', 'product_cat', $product_id)) {
        $product_is_gas = true;
    }

// 如果購物車為空則返回 true
    if (!WC()->cart->get_cart_contents_count() == 0) {
        // 如果購物車中包含【特定分類商品】並且要添加的產品不是【該分類】,則顯示錯誤消息並返回 false
        if ($cart_has_gas && !$product_is_gas) {
            wc_add_notice('錯誤消息撰寫在此', 'error');
            $validation = false;
        }
        // 如果購物車包含的產品不是【特定分類商品】,而要添加的產品是【該分類】,則顯示錯誤消息並返回false
        elseif (!$cart_has_gas && $product_is_gas) {
            wc_add_notice('錯誤消息撰寫在此', 'error');
            $validation = false;
        }
    }
    // 否則, 返回 true
    return $validation;
}

add_filter('woocommerce_add_to_cart_validation', 'dont_add_gas_to_cart_containing_other', 10, 2);

最後修改日期: 2022 年 11 月 15 日

作者