WordPress plugin called WooCommerce, which enables e-commerce functionality for WordPress websites, was discussed in detail in the article titled WordPress WooCommerce Online Shopping Plugin.
I’d like to add a few more features related to that article: conditional tags, page customization, theme integration, REST API usage, tracking (via Google Tag Manager; Google Analytics and Facebook Pixel for), and reporting. This article focuses on the first topic listed: conditional tags.
Conditional Tags
There are several special functions known as conditional tags that are frequently used in WordPress theme and plugin development. These functions allow us to define conditions based on the page type (e.g., search page, post, page, custom page, 404, etc.), whether a plugin is active, whether the site is multisite, whether comments are open, whether the user is logged in, or whether the user is an administrator. For example, when a blog post is published, it typically appears as a post page (post_type), while pages such as “About Us” or “Contact” usually appear as page-type pages. Therefore, when we want to perform actions specific to these types of pages or only active on such pages, we use predefined functions like is_single() or is_page(). When the condition is met, the relevant function returns true; otherwise, it returns false. Furthermore, these conditions can be restricted by values such as the post ID, title, post name, or post title, and can even be expanded into an array for multiple conditions.
This allows for numerous customizations within relevant content areas. Of course, conditional tags are not limited to just is_single() and is_page(). Other tags such as is_admin(), is_home(), is_front_page(), comments_open(), is_page_template(), is_category(), is_search(), is_404(), is_multisite(), is_plugin_active(), wp_script_is() and many more are available, as listed on the WordPress Codex > Conditional Tags page with examples[^1]. I’m not certain whether we should go into detail on this topic in depth, but please feel free to send me your questions via message.
Thus, let’s examine how conditional tags are handled in the context of WooCommerce.
Conditional Tags and Page Types
Under normal circumstances (for example, when the WooCommerce plugin is not installed), $post->post_type returns one of several types defined by WordPress:
- Post (post_type:
post, conditional_tag:is_single()) - Page (post_type:
page, conditional_tag:is_page()) - Attachment (post_type:
attachment, conditional_tag:is_attachment()) - Revision (post_type:
revision) - Navigation Menu (post_type:
nav_menu_item) - Custom CSS (post_type:
custom_css) - Changesets (post_type:
customize_changeset) - User Data Request (post_type:
user_request)
However, we can also create a custom post type using register_post_type(). WooCommerce does exactly this, setting the post type to product for product pages. Additionally, we gain the ability to use the conditional tag is_product() to perform operations on the page level, similar to how is_single() works. Let’s first examine how a custom post type can be defined. Below, you can see how a post type is defined with the name book:
function create_post_type() {
register_post_type( 'book',
array(
'labels' => array(
'name' => __( 'Books' ),
'singular_name' => __( 'Book' )
),
'public' => true,
'has_archive' => true,
'rewrite' => array('slug' => 'books'),
)
);
}
add_action( 'init', 'create_post_type' );
Once you place this code in the functions.php file, you’ll be able to see the Books heading in the left sidebar of the WP Admin panel. Yes, all we need to do to create a new post type is this[^2].
After this basic post type explanation (and yes, I’ll keep this brief), we can now discuss WooCommerce’s conditional tags, starting with those provided by WooCommerce, such as is_product(). For the book example, the post type will return book, while WooCommerce product pages will return the product value. WooCommerce provides customized tags to avoid dealing with post type queries or the is_single() function. The is_product() tag we mentioned earlier is just one of these[^3]. So, what other conditional tags can we use for WooCommerce operations, and for what purposes?
WooCommerce Conditional Tags
After reviewing the background information I thought appropriate, we can now examine the conditional tags provided by WooCommerce. Let’s start with is_product().
is_product()
This is included within is_singular(), and defines product page pages (returns true)[^4]. You can determine whether a page is a product page using is_product() outside of a post_type check, and then enable specific actions under that condition.
is_woocommerce()
If the page being displayed meets the condition and is a WooCommerce template, it will return true.
is_shop()
Product archive page (product listing) definition.
### is_product_category()
Returns `true` when any product category page is displayed.
### is_cart()
Returns `true` when the cart page is displayed.
### is_checkout()
Returns `true` when the checkout page is displayed.
### is_wc_endpoint_url()
Returns `true` when any WooCommerce endpoint (such as payment result page, order viewing, address editing, adding payment methods, etc.) is displayed. I'll also mention some of these endpoints specifically below.
#### is_wc_endpoint_url('order-pay')
Returns `true` when the order payment page is displayed.
#### is_wc_endpoint_ url('order-received')
Returns `true` once the payment has been completed.
#### is_wc_endpoint_url('view-order')
Returns `true` when an order is viewed. Of course, these conditions are not limited to just this! For more detailed information and examples, please refer to the _WooCommerce Docs_ > _Conditional Tags_ page[^5]. As mentioned earlier, these condition tags can be used for many different purposes. Specifically, I'll focus on the process of reporting e-commerce data to analytics platforms such as [Google Analytics](../google-analytics-gtag-ecommerce-events) and [Facebook Pixel and Standard Events](../facebook-pixel-and-standard-events). You can subscribe to my email newsletter for upcoming articles and video tutorials, and follow my [YouTube channel](https://www.youtube.com/ceaksan?target=_blank&rel=noopener,noreferrer) for updates.
[^1]: ["Conditional Tags", WordPress Codex](https://codex.wordpress.org/Conditional_Tags?target=_blank&rel=noopener,noreferrer)
[^2]: ["Post Types", WordPress Codex](https://codex.wordpress.org/Post_Types?target=_blank&rel=noopener,noreferrer)
[^3]: ["Function in Product", WooCommerce Docs](https://docs.woocommerce.com/wc-apidocs/function-is_product.html?target=_blank&rel=noopener,noreferrer)
[^4]: ["is_singular Function", Developer Wordpress](https://developer.wordpress.org/reference/functions/is_singular/?target=_blank&rel=noopener,noreferrer)
[^5]: ["Conditional Tags", WooCommerce Docs](https://docs.woocommerce.com/document/conditional-tags/?target=_blank&rel=noopener,noreferrer)