WooCommerce通知,只运行一次

时间:2016-11-03 15:53:14

标签: php wordpress woocommerce

我对php很新。 使用WooCommerce在Wordpress网站上工作。 我添加了一个通知,显示"订单超过$ 400免费送货...",它可以工作,但当一个项目被删除或添加到购物车时,它会多次显示。我希望它只显示一次。 这是我的代码:

function sp_custom_notice() {
    if ( ! is_cart() ) {
        return;
    }
    $subtotal = WC()->cart->get_cart_subtotal();
    $free_shipping_threshold = 400;
    $notice_freeship = wc_add_notice( 'Get FREE shipping on orders over $400. Only valid in the US, not valid on bullets. <a href="https://bulletcentral.com/shipping/#promo">For more information, click here</a>', 'notice' );
    if ( $subtotal < $free_shipping_threshold ) {
        return $notice_freeship;
    }
}

2 个答案:

答案 0 :(得分:1)

您使用sp_custom_notice函数添加了哪些操作?如果你使用

add_action('wp', 'sp_custom_notice')

它将被多次调用。

尝试与其他行动挂钩:

add_action( 'woocommerce_before_checkout_form', 'sp_custom_notice' );

因此,通知只会显示结帐页面的一次。

答案 1 :(得分:0)

你可以做一件事。执行操作时删除以前的通知并显示新的通知。

function sp_custom_notice() {
if ( ! is_cart() ) {
    return;
}
$subtotal = WC()->cart->get_cart_subtotal();
$free_shipping_threshold = 400;

wc_clear_notices(); //Add this line
$notice_freeship = wc_add_notice( 'Get FREE shipping on orders over $400. Only valid in the US, not valid on bullets. <a href="https://bulletcentral.com/shipping/#promo">For more information, click here</a>', 'notice' );
if ( $subtotal < $free_shipping_threshold ) {
    return $notice_freeship;
}}