skip to Main Content

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


  1. 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

    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
  3. 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)
      );
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search