Search code examples
phpwordpresswoocommercehook-woocommerce

CRITICAL Uncaught Error: Call to a member function get_description() on bool (woocommerce)


i needed to get a [product_description] shortcode to my products (they have a particular structure and tabs wans't right for me)so added to my functios file this piece of code

add_shortcode( 'custom_product_description', 'display_custom_product_description' );
function display_custom_product_description( $atts ){
    $atts = shortcode_atts( array(
        'id' => get_the_id(),
    ), $atts, 'custom_product_description' );

    global $product;

    if ( ! is_a( $product, 'WC_Product') )
        $product = wc_get_product($atts['id']);

    return $product->get_description();
}

work perfect for what i wanted, however; since i got this code i keeo getting this fatal error in logs:

2021-03-16T00:00:16+00:00 CRITICAL Uncaught Error: Call to a member function get_description() on bool in /home/evadevcl/public_html/elr/wp-content/themes/flatsome-child/functions.php:30 Stack trace: #0 /home/evadevcl/public_html/elr/wp-includes/shortcodes.php(343): display_custom_product_description(Array, '', 'custom_product_...') #1 [internal function]: do_shortcode_tag(Array) #2 /home/evadevcl/public_html/elr/wp-includes/shortcodes.php(218): preg_replace_callback('/\[(\[?)>>>>(custom...', 'do_shortcode_ta...', '\n[custom_produc...') #3 /home/evadevcl/public_html/elr/wp-content/themes/flatsome/inc/helpers/helpers-shortcode.php(203): do_shortcode('\n[custom_produc...') #4 /home/evadevcl/public_html/elr/wp-content/themes/flatsome/inc/shortcodes/row.php(214): flatsome_contentfix('\n[custom_produc...') #5 /home/evadevcl/public_html/elr/wp-includes/shortcodes.php(343): ux_col(Array, '\n[custom_produc...', 'col') #6 [internal function]: do_shortcode_tag(Array) #7 /home/evadevcl/public_html/elr/wp-includes/shortcodes.php(218): preg_replace_callback( en /home/evadevcl/public_html/elr/wp-content/themes/flatsome-child/functions.php en la línea 30

Even though i have search this exact issue i can´t seem to find the solution for it, it is not making any harm (visible or function) in my site but i am kind worried this might brake my site at any time, does anyone could help me with this?


Solution

  • You can add an initial check to verify that the shortcode only activates on the product page.

    add_shortcode( 'custom_product_description', 'display_custom_product_description' );
    function display_custom_product_description( $atts ){
    
        // only on the product page
        if ( ! is_product() ) {
            return;
        }
    
        $atts = shortcode_atts( array(
            'id' => get_the_id(),
        ), $atts, 'custom_product_description' );
    
        global $product;
    
        if ( ! is_a( $product, 'WC_Product') )
            $product = wc_get_product($atts['id']);
    
        return $product->get_description();
    }
    

    If you want to use the shortcode also outside the product page

    You don't have to set the get_the_id() value to the id attribute of the first parameter otherwise the shortcode will always ignore the product id entered by the user.

    You can use the following shortcode on any page of the site by setting, in this case, the id attribute with the id of the product you want to get the description of.

    You can optimize the shortcode like this:

    add_shortcode( 'custom_product_description', 'display_custom_product_description' );
    function display_custom_product_description( $atts ){
    
        $atts = shortcode_atts( array(
            'id' => '',
        ), $atts, 'custom_product_description' );
    
        if ( empty( $atts['id'] ) ) {
            if ( is_product() ) {
                global $product;
            } else {
                return;
            }
        } else {
            if ( is_a( wc_get_product($atts['id']), 'WC_Product' ) ) {
                $product = wc_get_product( $atts['id'] );
            } else {
                return;
            }
        }
    
        return $product->get_description();
    }