j.c
j.c

Reputation: 2882

Target checkout page but not order-received page in Woocommerce

I'm linking some .js in my functions.php this way:

if(get_query_var('pagename')==='checkout') {
    wp_enqueue_script('script', 'my.js', array('jquery'), 1.1, true);
}

my.js is where i check for some checkout form input fields.
Now i see that my.js is linked also in the order-received page (that is a 'sub-page' of the checkout page, i know!), and this causes some errors because the checkout form isn't there anymore.

How to conditionally execute some code in the checkout page but not in order-received one?

Thanx.

Upvotes: 2

Views: 2266

Answers (2)

LoicTheAztec
LoicTheAztec

Reputation: 253969

To target checkout page avoiding "order-received" or "order-pay" endpoints use:

if( is_checkout() && ! ( is_wc_endpoint_url( 'order-pay' ) || is_wc_endpoint_url( 'order-received' ) ) ) {
    wp_enqueue_script('script', 'my.js', array('jquery'), 1.1, true);
}

Tested and work.

Upvotes: 5

Ben
Ben

Reputation: 90

Try using is_checkout_pay_page() function:

if(is_checkout_pay_page()) {
    wp_enqueue_script('script', 'my.js', array('jquery'), 1.1, true);
}

Upvotes: 0

Related Questions