The is_checkout() method is the standard to build conditionally logic around on whether the user is on the checkout page or not.
But what if it is too early for is_checkout() to be available?
Paying attention to the WordPress load lifecycle, is_checkout is available “too late” in the cycle.
It may work when called in a function hooked to template_reditect or wp_head.
If you need it before the template has loaded, is_checkout renders completely useless.
So, you need to get creative to check whether the current page is the checkout page or not.
A solution I came up with recently for a project is like so:
add_action('wp_loaded', function(){
//Get the page ID from the WooCommerce option.
$checkout_page_id = get_option( 'woocommerce_checkout_page_id' );
// Transform the obtained string value into an integer.
$checkout_page_id = (int)$checkout_page_id;
// Get the Request Slug.
$request_uri = $_SERVER['REQUEST_URI'];
// Get the Post ID from the obtained slug.
$post_id = url_to_postid($request_uri);
// Compare the two IDs.
if( $post_id == $checkout_page_id ) {
exit('this is the checkout page'); // If the IDs match identically, then we are on the checkout page.
}
});
The example above uses a combination of retrieving the checkout page ID stored in the wp_options table as a WooCommerce option.
Then obtains the Page ID of the current query.
When the IDs match, we are on the checkout page.