Title: Fatal error for variable products with custom function
Last modified: August 31, 2023

---

# Fatal error for variable products with custom function

 *  Resolved [wzshop](https://wordpress.org/support/users/wzshop/)
 * (@wzshop)
 * [2 years, 9 months ago](https://wordpress.org/support/topic/fatal-error-for-variable-products-with-custom-function/)
 * Hi, I have a simple custom function for showing some addition information after
   the product description. You can find the code below. Now I have found that for
   variable products, the function below combined with your WPSSO core plugin is
   causing a fatal error. How to fix this?
 *     ```wp-block-code
       <?php
         add_filter('woocommerce_short_description','tw_add_text_short_descr');
   
         function tw_add_text_short_descr($description){
       	if (! is_product()) { return; }
       	global $product;
   
       	if (  $product->is_type( 'simple' )) {
   
       		$description = 'hi';
       	}
   
       	return $description;
         }
       ?>
       ```
   
 * The error is:
 *     ```wp-block-code
       Fatal error: Uncaught Error: Call to a member function is_type() on string in /xxxxxx/wp-content/child-theme/functions.php:24 
       Stack trace: #0 /xxxxxx/wp-includes/class-wp-hook.php(310): tw_add_text_short_descr('') 
       #1 /xxxxxx/wp-includes/plugin.php(205): WP_Hook->apply_filters('', Array) 
       #2 /xxxxxx/wp-content/plugins/woocommerce/includes/wc-formatting-functions.php(1121): apply_filters('woocommerce_sho...', '') 
       #3 /xxxxxx/wp-content/plugins/woocommerce/includes/class-wc-product-variable.php(398): wc_format_content('') 
       #4 /xxxxxx/wp-content/plugins/wpsso/lib/util-woocommerce.php(231): WC_Product_Variable->get_available_variation(Object(WC_Product_Variation)) 
       #5 /xxxxxx/wp-content/plugins/wpsso/lib/integ/ecom/wooco in /xxxxxx/wp-content/child-theme/functions.php on line 24
       ```
   

Viewing 2 replies - 1 through 2 (of 2 total)

 *  Plugin Author [JS Morisset](https://wordpress.org/support/users/jsmoriss/)
 * (@jsmoriss)
 * [2 years, 9 months ago](https://wordpress.org/support/topic/fatal-error-for-variable-products-with-custom-function/#post-17015976)
 * It sounds like you’ve made the assumption that the WooCommerce short description
   is only used in a specific place in your theme templates. The WooCommerce short
   description may be retrieved, and its filter applied, at any time during the 
   load process (before the $product global is defined, for example). Good practice
   is to check that things are available and they are what you expect them to be,
   for example:
 * if ( ! function_exists( ‘is_product’ ) || ! is_product() )
 * if ( is_object( $product ) && $product->is_type( ‘simple’ ) )
 * BTW, a filter should **always** return its input, so “return;” is not correct.
   If you return, no matter why, it should always be “return $description;”.
 * js.
 *  Thread Starter [wzshop](https://wordpress.org/support/users/wzshop/)
 * (@wzshop)
 * [2 years, 9 months ago](https://wordpress.org/support/topic/fatal-error-for-variable-products-with-custom-function/#post-17016058)
 * Hi, thanks a lot!
   I have updated the code with an extra check and now it is working.
   Code is something like:
 *     ```wp-block-code
       add_filter('woocommerce_short_description','tw_add_text_short_descr');
   
         function tw_add_text_short_descr($description) {
       	global $product;
       	if ( is_product() && is_a( $product, 'WC_Product' )) { 
   
       	if (  $product->is_type( 'simple' ) ) { 
       		$description = 'simple';
   
       	}
   
       	elseif ( $product->is_type( 'variable' ) )	{
       		$description = 'variable';
       	}
   
       	return $description;
          }
         }
       ```
   

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Fatal error for variable products with custom function’ is closed to 
new replies.

 * ![](https://ps.w.org/wpsso/assets/icon-256x256.png?rev=3167695)
 * [WPSSO Core - Complete Schema Markup and Meta Tags](https://wordpress.org/plugins/wpsso/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/wpsso/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/wpsso/)
 * [Active Topics](https://wordpress.org/support/plugin/wpsso/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/wpsso/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/wpsso/reviews/)

 * 2 replies
 * 2 participants
 * Last reply from: [wzshop](https://wordpress.org/support/users/wzshop/)
 * Last activity: [2 years, 9 months ago](https://wordpress.org/support/topic/fatal-error-for-variable-products-with-custom-function/#post-17016058)
 * Status: resolved