In a WordPress template, how can I detect if the current page is the WooCommerce cart or checkout page?
Asked Answered
T

3

9

I would like to have a header banner hidden when a user is in the WooCommerce cart or checkout process. Is there a flag or variable that I can check to see if the current page is in either of these WooCommerce sections? I basically want to do something like the following:

if (!is_checkout() && !is_cart()) {
   echo "<div>My Banner</div>";
}

I realize I can make a custom page template for each of these sections, but I just want to add a simple bit of code to my global site header.

Tindall answered 21/5, 2020 at 17:24 Comment(2)
Your code is good to check this condition. More details added in the answer.Shonda
Well, wouldn't you know, Woocommerce thought of everything! Thanks.Tindall
S
19

Cart page

is_cart()

Returns true on the cart page.

Checkout page

is_checkout()

Returns true on the checkout page.

You can see more about WooCommerce conditional tags

Shonda answered 21/5, 2020 at 17:52 Comment(4)
also... is_checkout() returns true on the "checkout complete screen"... and may result in undesired behavior if you have coded something only for the "checkout. screen"Creeper
@Creeper For that you can use if ( is_checkout() && !empty( is_wc_endpoint_url('order-received') ) )Shonda
thanks! i worked out my own condition... i just wanted to be a little more explicit for anyone else coming along...Creeper
i do like your method better... my method could remotely, potentially mismatch... but because is_wc_endpoint_url returns a bool... so i like this better as well... if ( is_checkout() && !is_wc_endpoint_url('order-received') )... i use empty() often and effectively in my code... but i don't believe there is an objective reason for using it in this case ? or is there... ?Creeper
N
5

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)
  );
}
Nudicaul answered 11/10, 2021 at 18:13 Comment(1)
Thanks for this Maciej, I was trying to check for checkout while loading the cart from session so this really helped me confirm my suspicion. You saved me time so kudos!Lynnelle
A
1

If you want to use a wordpress function, there is `is_page( 'name')' here for you. https://developer.wordpress.org/reference/functions/is_page/

You need to use the name (page slug) of your pages. In the example, I assume they are called 'cart and 'checkout'.

if (!is_page('cart') && !is_page('checkout')) { ... }

As in mujuonly s answer, you can also use what woocommerce offers you with conditional tags.

My answer may be an alternative if you are facing problems with woocommerce functions or want to stay with wordpress functions.

Adiabatic answered 21/5, 2020 at 22:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.