• Resolved bradleo

    (@bradleo)


    I got this error today :

    Uncaught Error: Call to a member function get_description() on bool in /home/customer/www/coinhodlerclub.com/public_html/wp-content/plugins/code-snippets/php/snippet-ops.php(446) : eval()'d code:12

    This is what I wrote:

    function display_product_description( $atts ){
       try { $atts = shortcode_atts( array(
            'id' => get_the_id(),
        ), $atts, 'product_description' );
    
        global $product;
    
        if ( ! is_a( $product, 'WC_Product') )
            $product = wc_get_product($atts['id']);
    		
    		return $product->get_description();
    	  } catch(\Exception $e) {
    	 echo $e->getMessage();
       }
    
        
    }
    add_shortcode( 'product_description', 'display_product_description' );
    

    I’m new to wordpress dev and would apprecita any suggestions in how to catch this probably and if any other ways would solve this.
    Thanks

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Shea Bunge

    (@bungeshea)

    wc_get_product is returning false, likely because the ID you are passing in is invalid. To avoid the error, you should add an additional check before fetching the description:

    function display_product_description( $atts ) {
    	$atts = shortcode_atts( array(
    		'id' => get_the_id(),
    	), $atts, 'product_description' );
    
    	global $product;
    
    	if ( ! is_a( $product, 'WC_Product' ) ) {
    		$product = wc_get_product( $atts['id'] );
    	}
    
    	if ( ! is_a( $product, 'WC_Product' ) ) {
    		return '';
    	}
    
    	return $product->get_description();
    }
    Thread Starter bradleo

    (@bradleo)

    @bungeshea lol makes sense , perfect thank you!

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

The topic ‘Uncaught Error: Call to a member function get_description()’ is closed to new replies.