How to Determine the WooCommerce Current Page

WooCommerce makes it a snap to programmatically determine the current page through the use of their Conditional Tags. These functions can be used to check whether the current page is the shop page: is_shop(), or a product page: is_product(). See WooCommerce Conditional for the full list.
These functions are based on the standard WordPress Conditional Tags, and thus behave in the same manner. Meaning that if you try to determine your current page from within a template file everything will work just fine, but if you need to figure out the page before then, from say an action within your plugin, you may find these wonderful conditional tags stop working for you. The reason for this is explained on the WordPress Conditional Tags page, but since it’s not totally obvious or intuitive when you’re first starting out, I figured I’d put this little post together to clarify the issue.
The WordPress/WooCommerce conditional tags won’t work until after the wp action is fired, making it the earliest point at which you can use the tags. You can view the entire list, and the order of the core WordPress actions at the Action Reference page.
add_action( 'wp', 'init' );
function init() {
if ( is_shop() ) {
// yipee, this works!
}
}
No comments yet.