WooCommerce Notice, run only once

2019-06-09 09:26发布

I'm fairly new to php. Working on a Wordpress site with WooCommerce. I added a notice that displays "Get free shipping on orders over $400...", and it works but when an item is removed or added to the cart it will display it multiple times. I would like it to only display once. Here's my code:

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

1条回答
放我归山
2楼-- · 2019-06-09 10:26

Which action do you add with the function sp_custom_notice? If you use

add_action('wp', 'sp_custom_notice')

It will be called multiple times.

Try to hook up with other action:

add_action( 'woocommerce_before_checkout_form', 'sp_custom_notice' );

So the notice will only shows one time when the checkout page is displayed.

查看更多
登录 后发表回答