woocommerce get product type

Products in  woocommerce   are divided into many different types in order to distinguish among them. Depending on the using purpose, product types are applied to search or filter product. If you are importing lots of products in woocommerce, you should pay attention to product type field. This article helps you know which type of the product you are viewing is. It looks at how woocommerce get product type.

Common product types

  • Simple: This type includes popular products without options, for example, a pen or a book
  • Group: This includes products in a set which can be bought separately. It just includes simple products, for example, a set of six glasses.
  • Virtual: It is the product type that does not require delivery or download. For example, services.
  • Download: These products also do not require delivery. They are downloaded to computers or other devices. They often are copyright music videos, press publications, etc.
  • External or Affiliate: These products are described but not sold on the website. People who work in Affiliate often use this type.
  • Variable: The product type includes lots of options and variables. For example, an L-blue t-shirt is a variable while an M-red t-shirt is another variable.

Display products

If you want to display products in the product detail page, you use hook woocommerce_single_product_summary

Therefore we write the function get_product_type to display,

The function to display information for groups is performed as below. You can add this content to the line containing echo code;

functionget_product_type() {
global $post;
if(function_exists('get_product') ){
$product = get_product( $post->ID );
if( $product->is_type( 'variable' ) ){
echo 'Product type is variable'  ;                }
if( $product->is_type( 'group' ) ){
echo 'Product type is Group  ;                }
if( $product->is_type( 'Virtual' ) ){
echo 'Product type is Virtual  ;
if( $product->is_type( 'download' ) ){
echo 'Product type is Download  ;                }

In order that this function works in hook woocommerce_single_product_summary, we add the following syntax to the function file

add_action( 'woocommerce_single_product_summary', 'get_product_type',  5 );

Now we get to check the result. Here is the result.

Depending on the using purpose, we can display it in collection to filter product by type.

We have solved the issue related to woocommerce get product type. If you have any question, please contact us or leave a message in the comment. We are willing to support you for free.

Enjoy this blog? Please spread the word :)