• Resolved wzshop

    (@wzshop)


    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?

    <?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:

    
    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

    (@jsmoriss)

    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

    (@wzshop)

    Hi, thanks a lot!
    I have updated the code with an extra check and now it is working. Code is something like:

    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.