WP SITES

3082 Coded Tutorials & 285 Plugins

Make WooCommerce block checkout coupon field open by default

Add this PHP code to your child themes functions file or custom code snippets plugin to make the coupon field open on page load by default.

By default, it’s closed.

add_action( 'wp_enqueue_scripts', 'bd_keep_coupon_field_open_inline', 20 );
function bd_keep_coupon_field_open_inline() {
    if ( is_checkout() ) {
        wp_register_script( 'bd-checkout-inline', '', [], '', true );
        wp_enqueue_script( 'bd-checkout-inline' );

        wp_add_inline_script( 'bd-checkout-inline', "
            jQuery(document).ready(function($) {
                const observer = new MutationObserver(function() {
                    const \$couponButton = $('.wc-block-components-panel__button');
                    if (\$couponButton.length && \$couponButton.attr('aria-expanded') === 'false') {
                        \$couponButton.trigger('click');
                        observer.disconnect();
                    }
                });

                observer.observe(document.body, {
                    childList: true,
                    subtree: true
                });
            });
        " );
    }
}

Related Coded Solutions

Leave a Reply

New Plugins