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:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' ); |
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:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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?'; | |
} |
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.
Leave a Reply