簡體   English   中英

如果購物車包含特定類別(WooCommerce),則阻止添加到購物車

[英]Prevent add to cart if cart contains a specific category (WooCommerce)

我有一個包含 5 個類別的商店。 它是多供應商,由於運輸的復雜性,當購物車中只有一個特定類別(“油漆”)的產品時,我只能銷售它們。 因此,當購物車包含油漆時,我需要防止將任何非油漆產品添加到購物車,並顯示錯誤消息,我需要防止將油漆產品添加到包含任何其他類別的購物車,並顯示錯誤消息。

我試圖從我在 Stackoverflow 和其他地方發現的片段中拼湊出這段代碼。 該邏輯似乎在我的大腦中起作用,但是當我嘗試實現代碼(通過 functions.php)時,它會阻止任何產品添加到購物車,除非購物車是空的。

這是我的代碼:

//*** Prevent mixture of paint and other prods in same cart ***//
function dont_add_paint_to_cart_containing_other() {

// Set flag false until we find a product in cat paint
$cat_check = false;

// Set $cat_check true if a cart item is in paint cat
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {

    $product = $cart_item['data'];

    if ( has_term( 'paint', 'product_cat', $product->id ) ) {
                $cat_check = true;
        // break because we only need one "true" to matter here
        break;
        }
}
// Return true if cart empty
if ( WC()->cart->get_cart_contents_count() == 0 ) {
    return true;
    }
    // If cart contains paint and product to be added is not paint, display error message and return false.
    elseif ( $cat_check && get_queried_object()->term_id != '245778522') {
        wc_add_notice( 'Sorry, you can only purchase paint products on their own. To purchase this product, please checkout your current cart or empty your cart and try again' , 'error' );
        return false;
    }
    // If cart contains a product that is not paint and product to be added is paint, display error message and return false.
    elseif (!$cat_check && get_queried_object()->term_id = '245778522') {
        wc_add_notice( 'Sorry, you can only purchase paint products on their own. To purchase this product, please checkout your current cart or empty your cart and try again' , 'error' );
        return false;
    }
    // Otherwise, return true.
    return true;
}

add_filter( 'woocommerce_add_to_cart_validation', 'dont_add_paint_to_cart_containing_other' );
//*** Prevent mixture of paint and other prods in same cart ***//
function dont_add_paint_to_cart_containing_other($validation, $product_id) {

// Set flag false until we find a product in cat paint
    $cart_has_paint = false;

// Set $cat_check true if a cart item is in paint cat
    foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) {

        $product = $cart_item['data'];

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

    $product_is_paint = false;
    if (has_term('paint', 'product_cat', $product_id)) {
        $product_is_paint = true;
    }

// Return true if cart empty
    if (!WC()->cart->get_cart_contents_count() == 0) {
        // If cart contains paint and product to be added is not paint, display error message and return false.
        if ($cart_has_paint && !$product_is_paint) {
            wc_add_notice('Sorry, you can only purchase paint products on their own. To purchase this product, please checkout your current cart or empty your cart and try again', 'error');
            $validation = false;
        }
        // If cart contains a product that is not paint and product to be added is paint, display error message and return false.
        elseif (!$cart_has_paint && $product_is_paint) {
            wc_add_notice('Sorry, you can only purchase paint products on their own. To purchase this product, please checkout your current cart or empty your cart and try again', 'error');
            $validation = false;
        }
    }
    // Otherwise, return true.
    return $validation;
}

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

改成這個

// If cart contains a product that is not paint and product to be added is paint, display error message and return false.
    elseif (!$cat_check && get_queried_object()->term_id = '245778522') {
        wc_add_notice( 'Sorry, you can only purchase paint products on their own. To purchase this product, please checkout your current cart or empty your cart and try again' , 'error' );
        return false;
    }

改變這個..你的if條件需要'=='

暫無
暫無

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

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