2016-12-05 2 views
0

Я создал фильтр в php для wordpress, который отображает специальное сообщение, когда продукт с определенным классом доставки находится в корзине. Специальное сообщение появляется несколько раз, так как многие товары этого класса находятся в корзине. Как я могу ограничить вывод только одним? Вот код:Как я могу ограничить ответ на php-фильтр только одним?

add_action('woocommerce_after_checkout_form', 'allclean_add_checkout_content', 12); 
 
function allclean_add_checkout_content() { 
 
    // set your special category name, slug or ID here: 
 
    $shippingclass = array('cut'); 
 
    $bool = false; 
 
    foreach (WC()->cart->get_cart() as $cart_item_key => $values) { 
 
\t $shipping_class = get_the_terms($values['variation_id'], 'product_shipping_class'); 
 

 
     if (isset($shipping_class[0]->slug) && in_array($shipping_class[0]->slug, $shippingclass)) { 
 
      $bool = true; 
 
    } 
 
    // If the special cat is detected in one items of the cart 
 
    // It displays the message 
 
    if ($bool) 
 
     echo '<div class="example1"><h3>Items in your cart can be cut to save on shipping. List which items you want cut in your order notes.</h3></div>'; 
 
} 
 
}

ответ

0

вы выводите его в цикле foreach. Просто переместите его снаружи. Кроме того, после того, как первый экземпляр найден, нет необходимости продолжать цикл, поэтому разбить его с break после установки $bool в true

add_action('woocommerce_after_checkout_form', 'allclean_add_checkout_content', 12); 

function allclean_add_checkout_content() 
{ 
    // set your special category name, slug or ID here: 
    $shippingclass = array('cut'); 
    $bool = false; 
    foreach (WC()->cart->get_cart() as $cart_item_key => $values) 
    { 
     $shipping_class = get_the_terms($values['variation_id'], 'product_shipping_class'); 

     if (isset($shipping_class[0]->slug) && in_array($shipping_class[0]->slug, $shippingclass)) 
     { 
      $bool = true; 
      break; 
     } 
    } 
    // If the special cat is detected in one items of the cart 
    // It displays the message 
    if ($bool) 
    { 
     echo '<div class="example1"><h3>Items in your cart can be cut to save on shipping. List which items you want cut in your order notes.</h3></div>'; 
    } 
} 
Смежные вопросы