WooCommerce Check Product Type

If you’re working with WooCommerce add-ons (e.g., Bundles), use this function to cycle between different WooCommerce Product Types, in order to display queries and functions in specific templates (operates similar to an if statement).

global $woocommerce;
// Get product type
$transient_name = 'wc_product_type_' . $product_id;
$product_type = '';
if ( false === ( $product_type = get_transient( $transient_name ) ) ) {
	$terms = wp_get_object_terms( $product_id, 'product_type', array('fields' => 'names') );
	$product_type = (isset($terms[0])) ? sanitize_title($terms[0]) : 'simple';
	set_transient( $transient_name, $product_type );
}


if ( $product_type == 'bundle' ) { 
       // query or function here
       // will display on Bundled Products templates only
} else {
       // query or function here
       // will display on default single Products template
}

Sourced here.

Leave a Reply

katherine as a flat graphic icon

About Me

I’m an African / Ojibwe First Nations Web Developer living in Winnipeg, Manitoba.

Visit the Tips and Blog to see what I’m working on.