Please note that is_checkout()
is useless if you need it early (before the template is loaded I think) and will incorrectly return false on checkout page if it's called too early.
Something like this is more universal:
/**
* Checks if checkout is the current page.
*
* @return boolean
*/
function better_is_checkout() {
$checkout_path = wp_parse_url(wc_get_checkout_url(), PHP_URL_PATH);
$current_url_path = wp_parse_url("http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]", PHP_URL_PATH);
return (
$checkout_path !== null
&& $current_url_path !== null
&& trailingslashit($checkout_path) === trailingslashit($current_url_path)
);
}
is_checkout() is fine for the actual question, but the question also ranks first in Google for "WooCommerce check if checkout"
The same applies for cart:
/**
* Checks if cart is the current page.
*
* @return boolean
*/
function better_is_cart() {
$cart_path = wp_parse_url(wc_get_cart_url(), PHP_URL_PATH);
$current_url_path = wp_parse_url("http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]", PHP_URL_PATH);
return (
$cart_path !== null
&& $current_url_path !== null
&& trailingslashit($cart_path) === trailingslashit($current_url_path)
);
}