skip to Main Content

My website started showing this strange error recently which I have been unable to resolve. When I try to view a product, it returns a critical error.

I’ve done some troubleshooting and I realized the theme is the culprit. I have also checked the error log and I saw that this specific line of code is causing the error:

Uncaught Error: Call to a member function is_in_stock() on string in /home/u306409103/domains/apdbrestore.com/public_html/wp-content/themes/custom-theme/woocommerce/single-product.php:45 Stack trace: #0

When you check that line, it says:

<?php if( !$product->is_in_stock() ) : ?>
                <div class="outofstock">
                    <img src="<?php echo imgfolder('out-of-stock.png'); ?>" alt="Out of Stock">
                </div>
            <?php endif; ?>

To be specific the error is pointing towards the first line:

<?php if( !$product->is_in_stock() ) : ?>

I know the problem is coming from here but don’t know what next to do. What statement can I use here to do away with this error?

Thanks

Full code

<?php

if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly
}

get_header( 'shop' ); ?>

    <?php
        /**
         * woocommerce_before_main_content hook.
         *
         * @hooked woocommerce_output_content_wrapper - 10 (outputs opening divs for the content)
         * @hooked woocommerce_breadcrumb - 20
         */
        do_action( 'woocommerce_before_main_content' );
    ?>
    <?php
        if( has_post_thumbnail() ) :
            $img = get_the_post_thumbnail_url();
        else :
            $img = placeholder();
        endif;
    ?>
<div class="product-single">
    <div class="container">
        <div class="row">
            <div class="col-md-7">
                <div class="product-single-left product-contain">
                    <?php if( !$product->is_in_stock() ) : ?>
                <div class="outofstock">
                    <img src="<?php echo imgfolder('out-of-stock.png'); ?>" alt="Out of Stock">
                </div>
            <?php endif; ?>
                    <div class="product-single-holder">
                        <div class="product-single-gallery">
                            <div class="product-single-image active" data-img="<?php echo $img; ?>" style="background-image: url(<?php echo $img; ?>);"></div>
                            <?php global $product;
                            $attachment_ids = $product->get_gallery_attachment_ids();
                                foreach( $attachment_ids as $attachment_id ) { ?>
                                   <div class="product-single-image" data-img="<?php echo wp_get_attachment_url( $attachment_id ); ?>" style="background-image: url(<?php echo $image_link = wp_get_attachment_url( $attachment_id ); ?>);"></div>
                            <?php } ?>
                        </div>
                    </div>
                    <div class="product-single-featured">
                        <img src="<?php echo $img; ?>" alt="<?php echo get_the_title(); ?>">
                    </div>
                </div>
            </div>
            <div class="col-md-5">
                <div class="product-single-content">
                    <h3><?php echo get_the_title(); ?></h3>
                    <div class="product-single-price">
                        <div class="home-sale-price">
                            <?php if( $product->get_sale_price() ) : ?>
                                        <div class="home-sale-less">
                                            Listed Price - <span>P <?php echo wc_price( wc_get_price_to_display( $product, array( 'price' => $product->get_sale_price() ) ) ); ?> </span>
                                        </div>
                                        <div class="home-sale-orig">
                                            <?php echo  'P '.wc_price( wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) ) ); ?>
                                        </div>
                                    <?php else : ?>
                                        <div class="home-sale-less">Listed Price - <span>P <?php echo wc_price( wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) ) ); ?> </span></div>
                                    <?php endif; ?>
                        </div>
                        <?php if(get_field('terms')): ?>
                            <div class="home-sale-term">Terms - <span>P <?php echo get_field('terms'); ?></span></div>
                        <?php endif; ?>
                         <?php if(get_field('dp_and_pay')): ?>
                            <div class="product-single-term"><?php echo get_field('dp_and_pay'); ?></div>
                        <?php endif; ?>
                    </div>
                    <div class="product-single-desc">
                        <?php echo apply_filters( 'the_content', $product->get_short_description() ); ?>
                    </div>
                    <a href="?add-to-cart=<?php echo get_the_ID(); ?>" data-quantity="1" class="button product_type_simple add_to_cart_button ajax_add_to_cart default-btn" data-product_id="<?php echo get_the_ID(); ?>" data-product_sku="" aria-label="Add “<?php echo get_the_title(); ?>” to your cart" rel="nofollow"><i class="fas fa-plus-square"></i>Add to Cart</a>
                </div>
                <?php while ( have_posts() ) : the_post(); ?>
                    <?php //wc_get_template_part( 'content', 'single-product' ); ?>
                <?php endwhile; // end of the loop. ?> 
            </div>
        </div>
    </div>
    <div class="home-sale home-new">
        <div class="container">
            <div class="row justify-content-center">
                <div class="col-md-10">
                    <div class="header-excerpt">
                        <?php $salehead = get_field('new_arrivals_content',8); ?>
                        <h3>Recommmended Items</h3>
                        <div class="header-excerpt-text"><?php echo $salehead['content']; ?></div>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-12">
                    <div class="home-sale-slider">
                        <?php
                            $args = array(
                                'post_type'      => 'product',
                                'posts_per_page' => -1,
                                'order_by' => 'date',
                                'order' => 'DESC'
                            );
                            $loop = new WP_Query( $args );
                            if ( $loop->have_posts() ) {
                                while ( $loop->have_posts() ) : $loop->the_post();
                                    if( has_post_thumbnail() ) :
                                        $img = get_the_post_thumbnail_url();
                                    else :
                                        $img = placeholder();
                                    endif;
                                    $product = wc_get_product(get_the_ID());
                                ?>
                                 <div class="home-sale-holder product-contain">
                                    <?php if( !$product->is_in_stock() ) : ?>
                                        <div class="outofstock">
                                            <img src="<?php echo imgfolder('out-of-stock.png'); ?>" alt="Out of Stock">
                                        </div>
                                    <?php endif; ?>
                                    <div class="home-sale-top">
                                        <img src="<?php echo $img; ?>" alt="<?php echo get_the_title(); ?>">
                                        <a href="?add-to-cart=<?php echo get_the_ID(); ?>" data-quantity="1" class="button product_type_simple add_to_cart_button ajax_add_to_cart default-btn default-btn-add" data-product_id="<?php echo get_the_ID(); ?>" data-product_sku="" aria-label="Add “<?php echo get_the_title(); ?>” to your cart" rel="nofollow"><i class="fas fa-plus-square"></i>Add to cart</a>
                                    </div>
                                    <div class="home-sale-bottom">
                                        <a href="<?php echo get_permalink(); ?>">
                                        <h5><?php echo get_the_title(); ?></h5>
                                        <div class="home-sale-price">
                                            <?php if( $product->get_sale_price() ) : ?>
                                                            <div class="home-sale-less">
                                                                Cash - <span>P <?php echo wc_price( wc_get_price_to_display( $product, array( 'price' => $product->get_sale_price() ) ) ); ?> </span>
                                                            </div>
                                                            <div class="home-sale-orig">
                                                                <?php echo  'P '.wc_price( wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) ) ); ?>
                                                            </div>
                                                        <?php else : ?>
                                                            <div class="home-sale-less">Cash - <span>P <?php echo wc_price( wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) ) ); ?> </span></div>
                                                        <?php endif; ?>
                                            
                                        </div>
                                        <?php if(get_field('terms')): ?>
                                            <div class="home-sale-term">Terms - <span>P <?php echo get_field('terms'); ?></span></div>
                                        <?php endif; ?>
                                        </a>
                                    </div>
                                 </div>  
                               <?php endwhile;
                            } else {
                                echo __( 'No products found' );
                            }
                            wp_reset_postdata();
                        ?>
                    </div>
                </div>
            </div>
        </div>
        <a href="<?php echo get_the_permalink(576) ?>" class="default-btn">View All Products</a>
    </div>
</div>
    <?php
        /**
         * woocommerce_after_main_content hook.
         *
         * @hooked woocommerce_output_content_wrapper_end - 10 (outputs closing divs for the content)
         */
        do_action( 'woocommerce_after_main_content' );
    ?>

    <?php
        /**
         * woocommerce_sidebar hook.
         *
         * @hooked woocommerce_get_sidebar - 10
         */
        do_action( 'woocommercve_sidebar' );
    ?>

<?php get_footer( 'shop' );

/* Omit closing PHP tag at the end of PHP files to avoid "headers already sent" issues. */

3

Answers


  1. Chosen as BEST ANSWER

    Finally got the solution to this problem and in case anyone stumbles across this, all I had to do was to update how to get the reference post ID because the existing one is deprecated already.

    <?php  global $product; 
                            $product = wc_get_product( $post->ID );
                    
                     $stock_quantity = get_post_meta($post->ID, '_stock', true);
                    //var_dump($product->get_stock_status());
                    
                    ?>
                    <?php if ($product->get_stock_status() != 'instock') : ?>
                        <div class="outofstock">
                            <img src="<?php echo imgfolder('out-of-stock.png'); ?>" alt="Out of Stock">
                        </div>
                    <?php endif; ?>
    

  2. Make sure that the $product is not a string or a boolean before checking for stock.

    <?php if( is_object($product) && !$product->is_in_stock() ) : ?>
    

    Or, you completly stop processing the template by bailing out at the top if no product was returned like this:

    if (!is_object($product)) {
     return;
    }
    
    Login or Signup to reply.
  3. Try to put this code, I updated your code a bit.

    <?php
    
    if ( ! defined( 'ABSPATH' ) ) {
        exit; // Exit if accessed directly
    }
    
    get_header( 'shop' );
     global $product;
     ?>
    
        <?php
            /**
             * woocommerce_before_main_content hook.
             *
             * @hooked woocommerce_output_content_wrapper - 10 (outputs opening divs for the content)
             * @hooked woocommerce_breadcrumb - 20
             */
            do_action( 'woocommerce_before_main_content' );
        ?>
        <?php
            if( has_post_thumbnail() ) :
                $img = get_the_post_thumbnail_url();
            else :
                $img = placeholder();
            endif;
        ?>
    <div class="product-single">
        <div class="container">
            <div class="row">
                <div class="col-md-7">
                    <div class="product-single-left product-contain">
                        <?php if( !$product->is_in_stock() ) : ?>
                    <div class="outofstock">
                        <img src="<?php echo imgfolder('out-of-stock.png'); ?>" alt="Out of Stock">
                    </div>
                <?php endif; ?>
                        <div class="product-single-holder">
                            <div class="product-single-gallery">
                                <div class="product-single-image active" data-img="<?php echo $img; ?>" style="background-image: url(<?php echo $img; ?>);"></div>
                                <?php
                                $attachment_ids = $product->get_gallery_attachment_ids();
                                    foreach( $attachment_ids as $attachment_id ) { ?>
                                       <div class="product-single-image" data-img="<?php echo wp_get_attachment_url( $attachment_id ); ?>" style="background-image: url(<?php echo $image_link = wp_get_attachment_url( $attachment_id ); ?>);"></div>
                                <?php } ?>
                            </div>
                        </div>
                        <div class="product-single-featured">
                            <img src="<?php echo $img; ?>" alt="<?php echo get_the_title(); ?>">
                        </div>
                    </div>
                </div>
                <div class="col-md-5">
                    <div class="product-single-content">
                        <h3><?php echo get_the_title(); ?></h3>
                        <div class="product-single-price">
                            <div class="home-sale-price">
                                <?php if( $product->get_sale_price() ) : ?>
                                            <div class="home-sale-less">
                                                Listed Price - <span>P <?php echo wc_price( wc_get_price_to_display( $product, array( 'price' => $product->get_sale_price() ) ) ); ?> </span>
                                            </div>
                                            <div class="home-sale-orig">
                                                <?php echo  'P '.wc_price( wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) ) ); ?>
                                            </div>
                                        <?php else : ?>
                                            <div class="home-sale-less">Listed Price - <span>P <?php echo wc_price( wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) ) ); ?> </span></div>
                                        <?php endif; ?>
                            </div>
                            <?php if(get_field('terms')): ?>
                                <div class="home-sale-term">Terms - <span>P <?php echo get_field('terms'); ?></span></div>
                            <?php endif; ?>
                             <?php if(get_field('dp_and_pay')): ?>
                                <div class="product-single-term"><?php echo get_field('dp_and_pay'); ?></div>
                            <?php endif; ?>
                        </div>
                        <div class="product-single-desc">
                            <?php echo apply_filters( 'the_content', $product->get_short_description() ); ?>
                        </div>
                        <a href="?add-to-cart=<?php echo get_the_ID(); ?>" data-quantity="1" class="button product_type_simple add_to_cart_button ajax_add_to_cart default-btn" data-product_id="<?php echo get_the_ID(); ?>" data-product_sku="" aria-label="Add “<?php echo get_the_title(); ?>” to your cart" rel="nofollow"><i class="fas fa-plus-square"></i>Add to Cart</a>
                    </div>
                    <?php while ( have_posts() ) : the_post(); ?>
                        <?php //wc_get_template_part( 'content', 'single-product' ); ?>
                    <?php endwhile; // end of the loop. ?> 
                </div>
            </div>
        </div>
        <div class="home-sale home-new">
            <div class="container">
                <div class="row justify-content-center">
                    <div class="col-md-10">
                        <div class="header-excerpt">
                            <?php $salehead = get_field('new_arrivals_content',8); ?>
                            <h3>Recommmended Items</h3>
                            <div class="header-excerpt-text"><?php echo $salehead['content']; ?></div>
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="col-12">
                        <div class="home-sale-slider">
                            <?php
                                $args = array(
                                    'post_type'      => 'product',
                                    'posts_per_page' => -1,
                                    'order_by' => 'date',
                                    'order' => 'DESC'
                                );
                                $loop = new WP_Query( $args );
                                if ( $loop->have_posts() ) {
                                    while ( $loop->have_posts() ) : $loop->the_post();
                                        if( has_post_thumbnail() ) :
                                            $img = get_the_post_thumbnail_url();
                                        else :
                                            $img = placeholder();
                                        endif;
                                        $new_product = wc_get_product(get_the_ID());
                                    ?>
                                     <div class="home-sale-holder product-contain">
                                        <?php if( !$new_product->is_in_stock() ) : ?>
                                            <div class="outofstock">
                                                <img src="<?php echo imgfolder('out-of-stock.png'); ?>" alt="Out of Stock">
                                            </div>
                                        <?php endif; ?>
                                        <div class="home-sale-top">
                                            <img src="<?php echo $img; ?>" alt="<?php echo get_the_title(); ?>">
                                            <a href="?add-to-cart=<?php echo get_the_ID(); ?>" data-quantity="1" class="button product_type_simple add_to_cart_button ajax_add_to_cart default-btn default-btn-add" data-product_id="<?php echo get_the_ID(); ?>" data-product_sku="" aria-label="Add “<?php echo get_the_title(); ?>” to your cart" rel="nofollow"><i class="fas fa-plus-square"></i>Add to cart</a>
                                        </div>
                                        <div class="home-sale-bottom">
                                            <a href="<?php echo get_permalink(); ?>">
                                            <h5><?php echo get_the_title(); ?></h5>
                                            <div class="home-sale-price">
                                                <?php if( $new_product->get_sale_price() ) : ?>
                                                                <div class="home-sale-less">
                                                                    Cash - <span>P <?php echo wc_price( wc_get_price_to_display( $new_product, array( 'price' => $new_product->get_sale_price() ) ) ); ?> </span>
                                                                </div>
                                                                <div class="home-sale-orig">
                                                                    <?php echo  'P '.wc_price( wc_get_price_to_display( $new_product, array( 'price' => $new_product->get_regular_price() ) ) ); ?>
                                                                </div>
                                                            <?php else : ?>
                                                                <div class="home-sale-less">Cash - <span>P <?php echo wc_price( wc_get_price_to_display( $new_product, array( 'price' => $new_product->get_regular_price() ) ) ); ?> </span></div>
                                                            <?php endif; ?>
                                                
                                            </div>
                                            <?php if(get_field('terms')): ?>
                                                <div class="home-sale-term">Terms - <span>P <?php echo get_field('terms'); ?></span></div>
                                            <?php endif; ?>
                                            </a>
                                        </div>
                                     </div>  
                                   <?php endwhile;
                                } else {
                                    echo __( 'No products found' );
                                }
                                wp_reset_postdata();
                            ?>
                        </div>
                    </div>
                </div>
            </div>
            <a href="<?php echo get_the_permalink(576) ?>" class="default-btn">View All Products</a>
        </div>
    </div>
        <?php
            /**
             * woocommerce_after_main_content hook.
             *
             * @hooked woocommerce_output_content_wrapper_end - 10 (outputs closing divs for the content)
             */
            do_action( 'woocommerce_after_main_content' );
        ?>
    
        <?php
            /**
             * woocommerce_sidebar hook.
             *
             * @hooked woocommerce_get_sidebar - 10
             */
            do_action( 'woocommercve_sidebar' );
        ?>
    
    <?php get_footer( 'shop' );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search