WooCommerce conditional tags – An overview

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:


function print_shop_banner( $content ) {
if ( is_shop() ) {
$content .= '<img src="http://domain.com/wp-content/uploads/myawesomebanner.png" />';
}
return $content;
}
add_filter( 'the_content', 'print_shop_banner' );

view raw

functions.php

hosted with ❤ by GitHub

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:


if ( is_product() ) {
echo 'Must have!';
} else if ( is_product_tag( array( 'tag1', 'tag2' ) ) ) {
echo 'Check these awesome products tagged "tag1" and "tag2"!';
} else if ( is_product_category() ) {
echo 'Don\'t you like the products in this category?';
}

view raw

functions.php

hosted with ❤ by GitHub

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.


More Posts That You Might Like…


4 responses to “WooCommerce conditional tags – An overview”

  1. valerie robinson Avatar

    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!

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

  2. Thank you very much!

    1. You’re welcome!

Leave a Reply

Categories

Newsletter

Receive new articles from this blog directly in your inbox!

No spam guaranteed!

Blog at WordPress.com.

%d bloggers like this: