2016-09-22 3 views
1

Ich habe eine Funktion, die zwei minimale und maximale Warenkorb Mengen für 2 verschiedene Artikelkategorien überprüft - eine minimale Menge hängt von der Anzahl der Artikel im Warenkorb der anderen Artikel Kategorie ab - und fügt auch hinzu ein Artikel automatisch für nicht eingeloggte Kunden. Wie kann ich "Nachrichten entfernen, rückgängig machen" unterdrücken und sicherstellen, dass "alle" Warnungen angezeigt werden? Ich habe den Code unten gegeben und alle Verbesserungen daran haben auch gewürdigt.Woocommerce Anzeige mehrerer Fehlermeldungen/Benachrichtigungen

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

Verwaltet, um die Nachricht Nachricht entfernt mithilfe von CSS zu entfernen. '.woocommerce-cart .woocommerce-Nachricht {display: none! wichtig;}' – Pradeep

Antwort

0

Verwaltet, um die Nachricht Element entfernt mithilfe von CSS zu entfernen. '.woocommerce-cart .woocommerce-message {display: none! wichtig;}'.

Verwandte Themen