2016-09-22 2 views
1

2 가지 아이템 카테고리에 대한 2 가지 최소 및 최대 카트 수량을 확인하는 기능이 있습니다. 하나의 최대 수량은 다른 아이템 카테고리의 카트에있는 숫자 항목에 따라 달라집니다. 고객이 로그인하지 않은 경우 자동으로 항목. '제거한 항목, 실행 취소'메시지를 표시하지 않고 '전체'경고가 표시되도록하려면 어떻게해야합니까? 나는 아래에 코드를 주었고 그 점에 대한 개선점도있다.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 {표시 : 없음! 중요;}' – Pradeep

답변

0

CSS를 사용하여 제거 된 메시지를 제거하도록 관리합니다. '.woocommerce-cart .woocommerce-message {표시 : 없음! 중요;}'.

관련 문제