Home » WooCommerce: is_single() Doesn’t Work [Solved]

WooCommerce: is_single() Doesn’t Work [Solved]

by Tutor Aspire

When building custom functions and using hooks and filters, you may want to apply those changes to a unique product id: “If product id = xxx, then execute this function”.

Honestly, I tried to do that with the simplest of all solutions (is_single WordPress conditional tag) but it didn’t work for me. Hopefully, I can help you solve this in 2 seconds!

WooCommerce problem: is_single() doesn’t work for product IDs

add_action( 'woocommerce_before_single_product', 'tutoraspire_run_this_for_specific_product_id' );

function tutoraspire_run_this_for_specific_product_id() {
if ( is_single( 123 ) ) {
 
// whatever you try to do here for product id = 123 won't work
 
}
 
}

Solution: check $product->get_id() instead

add_action( 'woocommerce_before_single_product', 'tutoraspire_run_this_for_specific_product_id' );

function tutoraspire_run_this_for_specific_product_id() {
global $product;
if ( $product->get_id() == 123 ) {
 
// whatever you try to do here for product id = 123 will work
 
}
 
}

You may also like