• Resolved miricky

    (@miricky)


    Hi, I’ve been trying to call a single product’s attributes via shortcode and use in my page builder. The code is below;

    add_shortcode("variation_table", "custom_available_variations_table");
    function custom_available_variations_table( $atts ) {
    
        global $post;
    
        // Attributes
        $atts = shortcode_atts(
            array(
                'id'    => $post->ID
            ),
            $atts, 'variation_table'
        );
    
        if( is_admin() ) return; // Only on front end
    
        $product = wc_get_product($atts['id']); // Get the WC_Product Object
    
        $output = '<div class="fs-product-data-wrapper">';
    
        // Variable products
        if( $product->is_type('variable'))
        {
            // Get available variations in the variable product
            $available_variations = $product->get_available_variations();
    
            if( count($available_variations) > 0 ){
                foreach( $available_variations as $variation )
                    $output .= format_product_data_output( $variation['variation_id'] );
            }
        }
        // Simple products
        elseif( $product->is_type('simple'))
        {
            $output .= format_product_data_output( $product->get_id() );
        }
        else return; // Exit
    
        return $output .= '</div>'; // return always for a shortcode
    }
    
    // Utility funtion: getting and formtting product data
    function format_product_data_output( $the_id ){
        $empty =  __( '<em>(empty)</em>', 'woocommerce' );
    
        // Get an instance of the WC_Product_Variation object
        $product = wc_get_product( $the_id );
    
        $durability = $product->get_attribute( 'pa_durability' );
        $durability = ! empty( $durability ) ? get_term_by( 'slug', $durability, 'pa_durability' )->name : $empty;
    
        $output = '
        <ul>
            <li class="fs-data-size">Durability: '.$durability.'</li>
    
        </ul>';
    
        return $output;
    }

    And below is the error I keep getting

    Uncaught Error: Call to a member function get_type() on bool in wp-content/themes/childtheme/functions.php:139
    Stack trace:
    #0 wp-includes/shortcodes.php(356): custom_available_variations_table(Array, '', 'variation_table')
    #1 [internal function]: do_shortcode_tag(Array)
    #2 wp-includes/shortcodes.php(228): preg_replace_callback('/\\[(\\[?)(variat...', 'do_shortcode_ta...', '[variation_tabl...')
    #3 wp-content/plugins/js_composer/include/helpers/helpers.php(240): do_shortcode('[variation_tabl...')
    #4 wp-content/plugins/js_composer/include/templates/shortcodes/vc_column_text.php(31): wpb_js_remove_wpautop('<p>[variation_t...', true)
    #5 wp-content/plugins/js_composer/include/classes/shortcodes/core/class-wpbakeryshortcode.php(271): require('/home/hypacart/...')
    #6 wp-content/plugins/js_composer/include/classes/shortcodes/co
Viewing 3 replies - 1 through 3 (of 3 total)
  • This means your $product variable is empty. And get_type() expects the product object. Check in your code why the $product variable does not contain the product object.

    Sometimes you can’t get the product type globally… as it depends on the WC_Product object.

    1) From a dynamic product id variable (when you doesn’t have the $product object:

    $product = wc_get_product( $product_id );
    or

    $product = wc_get_product( get_the_id() );
    2) From $product global variable on single product pages (and on product archive pages inside the loop):

    global $product;

    // Check that WC_Product instance $product variable is defined and accessible

    if ( ! is_a( $product, 'WC_Product' ) ) {
        $product = wc_get_product( get_the_id() );
    3) In cart items:

    // Loop throught cart items

    foreach( WC()->cart->get_cart() as $cart_item ){
        $product = $cart_item['data'];
    }

    3) In order items:

    // Loop through order items

    foreach ( $order->get_items() as $item ) {
        $product = $item->get_product();
    }
    Thread Starter miricky

    (@miricky)

    Problem was due to specifying the product type with is_type condition, which I don’t really need. I removed those conditions and I seems to be working

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Uncaught Error: Call to a member function get_type() on bool in /functions.php’ is closed to new replies.