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.
3
Answers
Cart page
Returns true on the cart page.
Checkout page
Returns true on the checkout page.
You can see more about WooCommerce conditional tags
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.
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:
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: