The conditional tags of WooCommerce and WordPress (also “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', 'bbloomer_echo_text' ); 

function bbloomer_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', 'bbloomer_echo_text' );

function bbloomer_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', 'bbloomer_single_product_pages' );

function bbloomer_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', 'bbloomer_single_product_ID' );

function bbloomer_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', 'bbloomer_single_category_slug' );

function bbloomer_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', 'bbloomer_single_tag_slug' );

function bbloomer_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', 'bbloomer_single_on_sale' );

function bbloomer_single_on_sale() {

if ( $product->is_on_sale() ) {
 // do something
}

}

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

add_action( 'woocommerce_after_single_product_summary', 'bbloomer_single_product_type' );

function bbloomer_single_product_type() {

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', 'bbloomer_single_virtual' );

function bbloomer_single_virtual() {

if( $product->is_virtual() ){
 // do something
} 

}

PHP: do something if product is downloadable

add_action( 'woocommerce_after_single_product_summary', 'bbloomer_single_downloadable' );

function bbloomer_single_downloadable() {

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', 'bbloomer_remove_variation_price', 10, 2 );

function bbloomer_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', 'bbloomer_loop_shop' );

function bbloomer_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', 'bbloomer_loop_per_product' );

function bbloomer_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', 'bbloomer_loop_cat' );

function bbloomer_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', 'bbloomer_loop_cat_slug' );

function bbloomer_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', 'bbloomer_loop_tag' );

function bbloomer_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', 'bbloomer_loop_tag_slug' );

function bbloomer_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)

add_action( 'woocommerce_before_main_content', 'bbloomer_woo_page' );

function bbloomer_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', 'bbloomer_cart_checkout' );

function bbloomer_cart_checkout() {

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

}

4. Are you working on the WooCommerce Cart/Checkout?

PHP: do something if WooCommerce Cart/Checkout has Product ID

function bbloomer_find_id_in_cart() {
foreach ( WC()->get_cart() as $cart_item_key => $values) {

    $product = $values['data'];

    if ( $product->get_id() == 123 ) {
        // do something
    }

}
}
Share:

Leave a Reply