Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Woocommerce - how to tell if product post has variations or not

Tags:

woocommerce

I am trying to edit the short-description template to be different on variable (single) product pages than on simple products. the code in that page is here:

global $post; if ( ! $post->post_excerpt )     return; ?> <div itemprop="description">     <?php echo apply_filters( 'woocommerce_short_description', $post->post_excerpt ) ?> </div> 

I want to add some code to the if statement that will be something like

if post has variations, don't display short description, if simple product DO display

but I can't find any way in the code to distinguish between a regular simple product post and one that is variable (has variations). And looking through the API docs over at the Woo site (http://docs.woothemes.com/wc-apidocs/) I found nothing of that sort.

like image 429
Stephen Avatar asked Jan 12 '14 14:01

Stephen


People also ask

Does the product have variations?

Product variations are the attribute used during checkout to identify specific products. For example, imagine you sell apparel online and a customer orders a t-shirt, and it comes in various sizes and colors. In this instance, the size and the color are the product variations for this t-shirt.

How do you show variable product price in WooCommerce?

Go to: WooCommerce > Products. Select the Add Product button or Edit an existing product. The Product Data displays. Select Variable product from the Product Data dropdown.


2 Answers

Use $product->is_type() function to check the product type. To check if the product is a variable product use:

global $product;  // $product->is_type( $type ) checks the product type, string/array $type ( 'simple', 'grouped', 'variable', 'external' ), returns boolean  if ( $product->is_type( 'variable' ) ) {} 

There is also $product->get_type() function that returns the internal type of a product as a string.

like image 158
Danijel Avatar answered Sep 21 '22 18:09

Danijel


After much heartache, I have found the following two solutions:

In the product loop, you can use this:

 if( $product->has_child() ) {  

but for some reason in the short description on the single product page, I had to use this:

global $post; $children = get_pages('child_of='.$post->ID); if( count( $children ) !== 0 ) { 

Hope this helps others that were struggling as I was...

like image 23
Stephen Avatar answered Sep 21 '22 18:09

Stephen