Two days ago I talked about WordPress conditional tags.
Also WooCommerce has conditional tags.

I use them even more than WordPress conditional tags, but that’s just because I mainly work on WooCommerce stuff like themes and plugins.
They are particularly useful in customizations, so if you want to add some custom features to your site, you should really learn how they work.

If you read some of my previous posts, you probably already noticed some of them in the snippets.
So let’s learn what we have and how we can use them.

Shop page & WooCommerce

The two main conditional tags are is_shop() and is_woocommerce().
is_shop() returns true if the page shown in the main Shop page of your store, that one set in WooCommerce > Settings > Products > Display. It could be useful to show, in example, a specific banner for the shop page only, depending on how you decide to add the banner to your store.

An example:

is_woocommerce() instead, is more generic. It returns true whenever you are on a page that uses a WooCommerce template. Note though that the Cart and Checkout pages are not included, neither those pages where you are only using a WooCommerce shortcode.

The snippet above applies here as well, if you want to show the banner in any WooCommerce page.

Products

There are several conditional tags related to products.

The most obvious is is_product(). It returns true if the page shown is a single product page.
But you can also check if you are on a specific product tag or category archive, with is_product_tag() and is_product_category().
They both accept a parameter, an array of terms or a string, to check if you are on a tag or category archive page for that term.

An example:

Other conditional tags

The remaining WooCommerce conditional tags are those needed to check if you are on the cart, checkout and account pages or on an endpoint.

To check if the page shown is the Cart page, you can use is_cart().
To check if the page shown is the Checkout page instead, use is_checkout(). This one is particularly useful for customizations for the payment gateways and shipping methods.
If you want to add/edit something only on the page My Account, use is_account_page(). Useful to print a special message for your customers in their account page.

If you want to do something on an endpoint instead, you can use is_wc_endpoint(). It accepts parameters to check if you are on a specific endpoint.
In example you could check if you are on the Thank you page by using is_wc_endpoint( 'order-received' ).

Learn more

If you want to learn more, you can check this page to know how to use the parameters accepted by these conditional tags.

4 replies
  1. valerie robinson
    valerie robinson says:

    Hey there–One question: is there a way to put a page ID instead of echo ‘Must have!’;

    I’m hoping to display the content from a WP page/post/cpt instead of ‘Must have!’

    Thanks!

    Reply
    • Nicola Mustone
      Nicola Mustone says:

      Hi Valerie,
      Yeah, “Must have!” was just an example, you can print anything you want there.

      If you want to print the page content, you may want to use the function get_post() to retrieve the page data and then print its content there.

      Reply

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

You have to agree to the comment policy.

This site uses Akismet to reduce spam. Learn how your comment data is processed.