2016-09-22 30 views
1

我有一個功能,可以檢查兩個不同項目類別的最小和最大購物車數量 - 一個最小最大數量取決於其他項目類別的購物車中存在的數量項目 - 一個項目自動未登錄客戶。我怎樣才能壓制'項目刪除,撤消'的消息,並確保'所有'我的警告顯示?我已經給出了下面的代碼和任何改進也讚賞。Woocommerce顯示多個錯誤消息/通知

function check_total() { 
// Only run in Cart or Checkout pages 
if(is_cart() || is_checkout()) { 

    global $woocommerce, $product; 
    $total_quantity = 0; 
    $total_squantity = 0; //snacks 
    $display_notice = 1; 
    $display_snotice = 1; 
    $i = 0; 

    //loop through all cart products 
    foreach ($woocommerce->cart->cart_contents as $product) { 
     // See if any product is from the breakfast or meals category or not 
     if (has_term('meals', 'product_cat', $product['product_id']) || has_term('breakfast', 'product_cat', $product['product_id'])) { 
      $total_quantity += $product['quantity']; 
     } 
     if (has_term('snacks', 'product_cat', $product['product_id'])) { 
      $total_squantity += $product['quantity']; 
     }  
    } 

    // Set up the acceptable meal totals. 
    $acceptable_totals = array(8, 9, 10, 11, 12, 20, 21, 22, 23, 24); 
    // Acceptable snacks totals 
    $acceptable_stotals = array(0, 1, 2, 3, 4); 

    foreach($acceptable_totals as $total_check) { 
     if ($total_check == $total_quantity) { $display_notice = 0; } 
    } 

    foreach($acceptable_stotals as $total_scheck) { 
     if ($total_scheck == $total_squantity) { $display_snotice = 0; } 
    } 

    foreach ($woocommerce->cart->cart_contents as $product) { 
     if (has_term('meals', 'product_cat', $product['product_id']) || has_term('breakfast', 'product_cat', $product['product_id'])) { 
      if($display_notice == 1 && $i == 0) { 
       // Display error message 
       wc_print_notice(sprintf('Whoa, you need to order 8-12 meals (1 cooler) or 20-24 meals (2 coolers). Give it another shot!<br />', $total_quantity),'error'); 
      } 
      $i++; 
     } 

    } 

    //Adjust snacks to match the meal quantities 
    if ((($total_quantity == 8) || ($total_quantity == 20)) && ($total_squantity > 4)){ 
      wc_print_notice(sprintf('Sorry dude, your bag can hold only 4 snack items...<br />', $total_squantity),'error'); 
     } 
     else if ((($total_quantity == 9) || ($total_quantity == 21)) && ($total_squantity > 3)){ 
      wc_print_notice(sprintf('Sorry dude, your bag can hold only 3 snack items...<br />', $total_squantity),'error'); 
     } 
     else if ((($total_quantity == 10) || ($total_quantity == 22)) && ($total_squantity > 2)){ 
      wc_print_notice(sprintf('Sorry dude, your bag can hold only 2 snack items...<br />', $total_squantity),'error'); 
     } 
     else if ((($total_quantity == 11) || ($total_quantity == 23)) && ($total_squantity > 1)){ 
      wc_print_notice(sprintf('Sorry dude, your bag can hold only 1 snack item...<br />', $total_squantity),'error'); 
     } 
     else if ((($total_quantity == 12) || ($total_quantity == 24)) && ($total_squantity > 0)){ 
      wc_print_notice(sprintf('Sorry dude, your bag(s) do not have enough space for snacks...<br />', $total_squantity),'error'); 
     } 

    // set our flag to be false until we find a product in that category 
    $cat_check = false; 

    // check each cart item for cooler bags category 
    foreach ($woocommerce->cart->cart_contents as $product) { 
     if (has_term('cooler-bags', 'product_cat', $product['product_id'])) { 
      $cat_check = true; 
      break; 
     }  
    } 

    // if a product in the cart is in our category, do something 
    if ($cat_check) { 
    // we have the category, do what we want 
    } 
    else { 
     if (is_user_logged_in()) { 
     //do nothing 
     } 
     else { 
      // select ID 
      $product_id = 148; 
      WC()->cart->add_to_cart($product_id); 
      // Display notification 
      wc_print_notice(__('We just added a cooler bag to your order as you seem to be new around here. Not new? <a href="/my-account">Click here to login</a>', 'woocommerce'), 'notice'); 
     } 
    } 
} 
} 
+0

管理刪除項目刪除消息使用CSS。 '.woocommerce-cart .woocommerce-message {display:none!important;}' – Pradeep

回答

0

管理刪除項目刪除消息使用CSS。 '.woocommerce-cart .woocommerce-message {display:none!important;}'。