2017-07-19 219 views
0

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

我試着湊齊這些代碼片段,我發現躺在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'); 

回答

1
//*** 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); 

更改這個

+0

謝謝你,ashvek。你是溫柔的人和學者。 –

+0

請標記答案的權利 – ashvek

0
// 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; 
    } 

更改這個..你如果病情需要 '=='

+0

感謝ashvek。當然,你是對的,但這並沒有完全解決我的問題。 隨着您的更改,我現在不能將任何產品添加到包含油漆的購物車,甚至是另一種油漆產品,並且我可以將任何產品(包括油漆產品)添加到包含其他類別的購物車。 有沒有其他想法? 非常感謝您的幫助 –

相關問題