WooCommerce reviews + tutorials

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!

  }

}

Published by Justin Stern

Justin is one of our co-founders, and is our resident overengineer. He likes to write developer tutorials and make black magic happen in our plugins. He thinks that writing code is a lot easier than writing words.

6 Comments

  1. Great, quick advice. Just what I needed. Thanks!

  2. Great!, Thanks

  3. This is an excellent article but the link to the WooCommerce documentation is broken now. It should be http://docs.woocommerce.com/document/conditional-tags/

  4. Cooooolllll, Thanks 🙂

  5. Hi,
    I am trying to wrap a function to change the quantity input on single product pages to a drop down selector. The function works. http://gerhardpotgieter.com/2013/09/09/woocommerce-product-quantity-dropdown/
    I want it to only load on single product pages.
    So I used your code with is_product()
    When I do a simple echo it works. When I add the selector code inside yours I get an error. I do not get that error when I run the selector code without wrapping it in your code.
    Can I fix this?
    The error is…
    Fatal error: Cannot redeclare woocommerce_quantity_input() (previously declared in /home/intervo/public_html/intervo_wp_site/wp-content/plugins/woocommerce/includes/wc-template-functions.php:860) in /home/intervo/public_html/intervo_wp_site/wp-content/themes/intervotion/functions.php on line 126

Hmm, looks like this article is quite old! Its content may be outdated, so comments are now closed.