2017-04-19 110 views
0

我想知道,在WordPress的+ WooCommerce:如何從購物車計數中刪除特定的產品或類別?如何從購物車計數中刪除產品或類別?

我在銷售與第二個產品(訂閱)關聯的產品,並且當我將此產品的5個添加到Basket中時,標題中的Mini Cart顯示了10個產品。

這可能會讓顧客感到害怕。我看了一下類似的問題,但對於標題中的購物車不起作用。

回答

0

您可以通過woocommerce_cart_contents_count篩選車數量。更改slug_of_category_to_ignore以適應。

/** 
* Filters the reported number of cart items. 
* Counts only items NOT in certain category. 
* 
* @param int $count 
* @return int 
*/ 
function so_43498002_cart_contents_count($count) { 

    $cart_items = WC()->cart->get_cart(); 
    $subtract = 0; 

    foreach ($cart_items as $key => $value) { 

     if (has_term('slug_of_category_to_ignore', 'product_cat', $value['product_id'])) { 
      $count -= $value[ 'quantity' ]; 
     } 
    } 

    return $count; 
} 
add_filter('woocommerce_cart_contents_count', 'so_43498002_cart_contents_count'); 
+0

感謝您的快速答覆。我明天會試試這個。 Semms在function.php –

+0

是的,或者一個網站特定的片段插件。 – helgatheviking

0

嘗試這個

add_action('woocommerce_check_cart_items', 'woocommerce_check_cart_quantities'); 
function woocommerce_check_cart_quantities() { 
    $total_products = WC()->cart->cart_contents_count; 
    $multiples = 6; 
    $totale = 0; 
    foreach (WC()->cart->get_cart() as $cart_item_key => $values) { 
     $prodotti = $values['data']; 

     if(! has_term(array(169, 152), 'product_cat', $prodotti->id)){ 
      $totale += $values['quantity']; 
     } 

    } 
    echo $totale; 
    if (($totale % $multiples) > 0){ 
     wc_add_notice(sprintf(__('You need to buy in multiples of %d products', 'your-textdomain'), $multiples), 'error'); 
    } 

} 
+0

謝謝你會嘗試。我承認來自另一篇文章的類別編號。 :) –

相關問題