How to Check If Product is Variable In WooCommerce

woocommerce check if product is variable

In a recent WooCommerce development project, I came across the need to check if the product is variable in WooCommerce, I needed to create an easy and quick way to conditionally add a custom option for the variable products in WooCommerce single product page.

I created the solution I will share with you in this quick guide and also WooCommerce check if the product is a variable code snippet that you can reuse in your WooCommerce development projects or customization of WooCommerce to help you save time and be more productive.

 WooCommerce Check If Product is Variable

To check if this is a variable product in WooCommerce you should make use of the is_type method of WooCommerce

 
WC_Product::is_type( $type );

You can use the function to check if the product is variable as in the code below :

 

// First get the product object that you will use to call the method is_type

$product = new WC_Product( get_the_ID() );

// Now check if the product is a variable product using

if( $product->is_type( 'variable' ) ){

// If this is a variable product do something

}

As you can see in the code above the first step is to get the product object that you will use to call the method is_type and the second step is to apply the is_type method to check if the product is a variable product.

You can extend this logic and check other products types as in the code below :

 
// First get the product object that you will use to call the method is_type

$product = new WC_Product( get_the_ID() );

// Now check if the product is a variable product using

if( $product->is_type( 'variable' ) ){

// If this is a variable product do something

}

// Now check if the product is a simple product using

if( $product->is_type( 'simple' ) ){

// If this is a simple product do something

}

Conclusion

I guess you can now comfortably check if a WooCommerce product is a variable product by following the steps I have outlined in this post.

The take from this quick guide: to check if the product is a variable product, you should make use of the is_type function that is available in WooCommerce.

If you need a custom solution to display WooCommerce products, I have several other solutions I have created and I would be glad to help you.

Feel free to reach out for further assistance. Use the contact form on this page and I will immediately get in touch.

Similar Articles