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.
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.
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.
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.
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...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With