Refresh the checkout page when a coupon is added or removed in WooCommerce

Refresh the checkout page when a coupon is added or removed in WooCommerce on child sites.

Snippet Type

Execute on Child Sites

Snippet

add_action( 'woocommerce_checkout_init', 'autoreload_checkout_on_coupon_event' );
function autoreload_checkout_on_coupon_event() {
    // Set the correct timeout value (in milliseconds)
    $timout1 = 600; // when applying a coupon
    $timout2 = 1500; // when removing a coupon
 
    wc_enqueue_js( "$.ajaxSetup({
        beforeSend: function(j, s) {
            if(s.url === '/?wc-ajax=apply_coupon' || s.url === '/?wc-ajax=remove_coupon') {
                var time = s.url === '/?wc-ajax=apply_coupon' ? {$timout1} : {$timout2};
                setTimeout(function(){
                    location.reload();
                }, time);
            }
        }
    });");
}

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.