2017-05-15 6 views
-1

Ich habe den Codeanzeigen wc_add_notice nur einmal

add_action('woocommerce_add_to_cart_validation', 'custom_add_to_cart_validation', 10, 3); 
function custom_add_to_cart_validation($passed, $product_id, $quantity) { 
    $_product = wc_get_product($product_id); 
    $quantity_total = (array_sum($_POST['quantity'])); 
    // echo $quantity_total; 

    if ($quantity_total % 2 != 0) { 
     wc_add_notice(__('Multiple of 2 required quantity.', 'woocommerce'), 'error'); 
     $passed = false; 
    } 
    else{ 
     $passed = true; 
    }var_dump($quantity_total); 

    return $passed; 
} 

Ich habe eine gruppierte Produkt mit vielen Elementen in & für jedes Produkt, wenn die Bedingung, es ist wahr das ich erhalte notice..but Ich möchte zeige es nur einmal an. Jetzt wird für jeden Eingang eine Fehlermeldung angezeigt. Von der Seite jedes Produkts möchte ich nur ein einziges Mal anzeigen.

+0

Verwenden Sie 'wc_clear_notices();' vor 'wc_add_notice();'. –

+0

@RaunakGupta du bist ein Genie! Setzen Sie Ihre Lösung als Antwort, um Ihnen +1 zu geben. – amarelinha12

Antwort

0

Wenn Sie alle vorherigen Nachrichten entfernen möchten, dann müssen Sie wc_clear_notices() vor wc_add_notice() verwenden.

So sollte der Code wie folgt aussehen:

add_action('woocommerce_add_to_cart_validation', 'custom_add_to_cart_validation', 10, 3); 

function custom_add_to_cart_validation($passed, $product_id, $quantity) 
{ 
    //... 
    //... 
    if ($quantity_total % 2 != 0) 
    { 
     wc_clear_notices(); //<--- check this line. 
     wc_add_notice(__('Multiple of 2 required quantity.', 'woocommerce'), 'error'); 
     $passed = false; 
    } 
    //... 
    //... 
} 

hoffe, das hilft!