Home » WooCommerce Conditional Logic – Tags, Examples & PHP

WooCommerce Conditional Logic – Tags, Examples & PHP

by Tutor Aspire

The WooCommerce and WordPress conditional tags (“WooCommerce and WordPress Conditional Logic”) can be used in your functions.php to display content based on certain conditions. For example, you could display different content for different categories within a single PHP function.

You can find the list of WooCommerce conditional tags here and WordPress conditional tags here. I don’t think it’s worth it to paste them here, so please use those two links as a reference – in fact in this blog post I would like to give you EXAMPLES, probably the best way for you to learn WooCommerce customization.

First: How to Use Conditional Logic

You may have a PHP snippet (any of the ones you find on this website for example), that does something like this:

add_action( 'woocommerce_before_single_product', 'tutoraspire_echo_text' ); 

function tutoraspire_echo_text() { 
   echo 'SOME TEXT'; 
} 

As you can see, this prints some text on top of the single product page. Conditional logic means that you want to run the function ONLY given a certain condition, for example in this case we want to print that text ONLY if the product ID is 25.

The way you need to modify the above function is therefore by “wrapping” the whole inside of the function inside a conditional check: if it’s product 25 do this, if not do nothing.

The function will therefore become:

add_action( 'woocommerce_before_single_product', 'tutoraspire_echo_text' );

function tutoraspire_echo_text() {
   global $product;
   if ( 25 === $product->get_id() ) {
      echo 'SOME TEXT';
   }
}

As you can see, the “echo” only happens if the condition is true. Now, keep reading for more conditional logic examples!

1. Are you working on the WooCommerce Single Product Page?

Great thing about single product pages in WooCommerce is that WordPress knows they are “posts”. So, you can use is_single. The list of hooks for the single product page can be found here.

PHP: do something on single product pages only

add_action( 'woocommerce_before_main_content', 'tutoraspire_single_product_pages' );

function tutoraspire_single_product_pages() {

if ( is_product() ) {
echo 'Something';
} else {
echo 'Something else';
}

}

PHP: do something if product ID = XYZ

add_action( 'woocommerce_after_single_product_summary', 'tutoraspire_single_product_ID' );

function tutoraspire_single_product_ID() {

if ( is_single( '17' ) ) {
echo 'Something';
} elseif ( is_single( '56' ) ) {
echo 'Something else';
}

}

PHP: do something if product belongs to a category

add_action( 'woocommerce_after_single_product_summary', 'tutoraspire_single_category_slug' );

function tutoraspire_single_category_slug() {

if ( has_term( 'chairs', 'product_cat' ) ) {
echo 'Something';
} elseif ( has_term( 'tables', 'product_cat' ) ) {
echo 'Something else';
}

}

PHP: do something if product belongs to a tag

add_action( 'woocommerce_after_single_product_summary', 'tutoraspire_single_tag_slug' );

function tutoraspire_single_tag_slug() {

if ( has_term( 'blue', 'product_tag' ) ) {
echo 'Something';
} elseif ( has_term( 'red', 'product_tag' ) ) {
echo 'Something else';
}

}

PHP: do something if product is on sale

add_action( 'woocommerce_after_single_product_summary', 'tutoraspire_single_on_sale' );

function tutoraspire_single_on_sale() {
global $product;
if ( $product->is_on_sale() ) {
 // do something
}

}

PHP: do something if product is simple, variable, external…

add_action( 'woocommerce_after_single_product_summary', 'tutoraspire_single_product_type' );

function tutoraspire_single_product_type() {
global $product;
if( $product->is_type( 'simple' ) ){
 // do something
} elseif( $product->is_type( 'variable' ) ){
 // do something
} elseif( $product->is_type( 'external' ) ){
 // do something
} elseif( $product->is_type( 'grouped' ) ){
 // do something
} 

}

PHP: do something if product is virtual

add_action( 'woocommerce_after_single_product_summary', 'tutoraspire_single_virtual' );

function tutoraspire_single_virtual() {
global $product;
if( $product->is_virtual() ){
 // do something
} 

}

PHP: do something if product is downloadable

add_action( 'woocommerce_after_single_product_summary', 'tutoraspire_single_downloadable' );

function tutoraspire_single_downloadable() {
global $product;
if ( $product->is_downloadable() ){
 // do something
} 

}

PHP: do something on the related products only

Related products are generated by a “loop”. Sometimes you might want to use your PHP on the single product page only (and excluding the related ones) or viceversa.

The snippet below hides the price only on the single product page and only on the related products section.

add_filter( 'woocommerce_variable_price_html', 'tutoraspire_remove_variation_price', 10, 2 );

function tutoraspire_remove_variation_price( $price ) {

global $woocommerce_loop;

if ( is_product() && $woocommerce_loop['name'] == 'related' ) {
$price = '';
}

return $price;
}

2. Are you working on the WooCommerce Shop/Category Page?

You can find all the shop/archive WooCommerce hooks here. Let’s see how to use conditional logic on these “loop” pages:

PHP: do something on the Shop page only

add_action( 'woocommerce_before_main_content', 'tutoraspire_loop_shop' );

function tutoraspire_loop_shop() {

if ( is_shop() ) {
echo 'This will show on the Shop page';
} else {
echo 'This will show on all other Woo pages';
}

}

PHP: do something on each product on the loop pages

add_action( 'woocommerce_after_shop_loop_item', 'tutoraspire_loop_per_product' );

function tutoraspire_loop_per_product() {

if ( has_term( 'chairs', 'product_cat' ) ) {
echo 'Great chairs!';
} elseif ( has_term( 'tables', 'product_cat' ) ) {
echo 'Awesome tables!';
}

PHP: do something on category pages only

add_action( 'woocommerce_before_main_content', 'tutoraspire_loop_cat' );

function tutoraspire_loop_cat() {

if ( is_product_category() ) {
echo 'This will show on every Cat pages';
} else {
echo 'This will show on all other Woo pages';
}

}

PHP: do something based on category name

add_action( 'woocommerce_before_main_content', 'tutoraspire_loop_cat_slug' );

function tutoraspire_loop_cat_slug() {

if ( is_product_category( 'books' ) ) {
echo 'This will show on the Books Cat page';
} elseif ( is_product_category( 'chairs' ) ) {
echo 'This will show on the Chairs Cat page';
}

}

PHP: do something on tag pages only

add_action( 'woocommerce_before_main_content', 'tutoraspire_loop_tag' );

function tutoraspire_loop_tag() {

if ( is_product_tag() ) {
echo 'This will show on every Cat pages';
} else {
echo 'This will show on all other Woo pages';
}

}

PHP: do something based on tag name

add_action( 'woocommerce_before_main_content', 'tutoraspire_loop_tag_slug' );

function tutoraspire_loop_tag_slug() {

if ( is_product_tag( 'red' ) ) {
echo 'This will show on the Red Tag page';
} elseif ( is_product_tag( 'yellow' ) ) {
echo 'This will show on the Yellow Tag page';
}

}

3. Are you working on WooCommerce Pages?

PHP: do something if on a WooCommerce page (excluding cart/checkout e.g. shop & cats & tags & products)

add_action( 'woocommerce_before_main_content', 'tutoraspire_woo_page' );

function tutoraspire_woo_page() {
   if ( is_woocommerce() ) {
      echo 'This will show on Woo pages';
   } else {
      echo 'This will show on WP pages';
   }
}

PHP: do something if on Cart/Checkout

add_action( 'woocommerce_sidebar', 'tutoraspire_cart_checkout' );

function tutoraspire_cart_checkout() {
   if ( is_cart() ) {
      echo 'This will show on the Cart sidebar';
   } elseif ( is_checkout() ) {
      echo 'This will show on the Checkout sidebar';
   }
}

PHP: do something if on Checkout Order Pay page

add_action( 'hook', 'tutoraspire_orderpay' );

function tutoraspire_orderpay() {
   if ( is_checkout_pay_page() ) {
      echo 'This will show on Order Pay page';
   } else {
      echo 'This will show on all other pages';
   }
}

PHP: do something if on My Account pages

add_action( 'hook', 'tutoraspire_myaccount' );

function tutoraspire_myaccount() {
   if ( is_account_page() ) {
      echo 'This will show on My Account pages';
   } else {
      echo 'This will show on pages different than My Account';
   }
}

PHP: do something if on Thank You Page

You could easily run functions on the thank you page by using the woocommerce_thankyou hook:

add_action( 'woocommerce_thankyou', 'tutoraspire_run_function_thankyou_page' );
 
function tutoraspire_run_function_thankyou_page() {
   // whatever
}

Otherwise, you can use another conditional, so that you know you’re on the “thank you page endpoint”:

add_action( 'wp_head', 'tutoraspire_run_function_thankyoupage' );

function tutoraspire_run_function_thankyoupage() {
   if ( is_wc_endpoint_url( 'order-received' ) {
      // whatever
   }
}

You may also like