只显示一次wc_add_notice [英] Show wc_add_notice only one time

查看:192
本文介绍了只显示一次wc_add_notice的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码

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;
}

我有一个分组的产品,在&中有很多项目对于每种产品,如果条件为真,我会收到此通知..但是我只想显示一次.现在从每个产品的页面显示每个输入..的错误消息,我只想显示一次.

I have a grouped product with many items in & for each product if the condition it's true i'm getting this notice..but I want to display it just one time. Now is displayed error message for each input .. from page of each product, what I want is to display just a single time.

推荐答案

如果要删除所有先前的消息,则必须使用 在 wc_clear_notices() .woocommerce.com/wc-apidocs/function-wc_add_notice.html"rel =" nofollow noreferrer> wc_add_notice() .

If you want to remove all the previous messages then you have to use wc_clear_notices() before wc_add_notice().

因此您的代码应如下所示:

So your code should look like this:

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;
    }
    //...
    //...
}

希望这会有所帮助!

这篇关于只显示一次wc_add_notice的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆