Home > other >  Critical error on single product page woo commerce
Critical error on single product page woo commerce

Time:09-26

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 >
                    <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 >
    <div >
        <div >
            <div >
                <div >
                    <?php if( !$product->is_in_stock() ) : ?>
                <div >
                    <img src="<?php echo imgfolder('out-of-stock.png'); ?>" alt="Out of Stock">
                </div>
            <?php endif; ?>
                    <div >
                        <div >
                            <div  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  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 >
                        <img src="<?php echo $img; ?>" alt="<?php echo get_the_title(); ?>">
                    </div>
                </div>
            </div>
            <div >
                <div >
                    <h3><?php echo get_the_title(); ?></h3>
                    <div >
                        <div >
                            <?php if( $product->get_sale_price() ) : ?>
                                        <div >
                                            Listed Price - <span>P <?php echo wc_price( wc_get_price_to_display( $product, array( 'price' => $product->get_sale_price() ) ) ); ?> </span>
                                        </div>
                                        <div >
                                            <?php echo  'P '.wc_price( wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) ) ); ?>
                                        </div>
                                    <?php else : ?>
                                        <div >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 >Terms - <span>P <?php echo get_field('terms'); ?></span></div>
                        <?php endif; ?>
                         <?php if(get_field('dp_and_pay')): ?>
                            <div ><?php echo get_field('dp_and_pay'); ?></div>
                        <?php endif; ?>
                    </div>
                    <div >
                        <?php echo apply_filters( 'the_content', $product->get_short_description() ); ?>
                    </div>
                    <a href="?add-to-cart=<?php echo get_the_ID(); ?>" data-quantity="1"  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 ></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 >
        <div >
            <div >
                <div >
                    <div >
                        <?php $salehead = get_field('new_arrivals_content',8); ?>
                        <h3>Recommmended Items</h3>
                        <div ><?php echo $salehead['content']; ?></div>
                    </div>
                </div>
            </div>
            <div >
                <div >
                    <div >
                        <?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 >
                                    <?php if( !$product->is_in_stock() ) : ?>
                                        <div >
                                            <img src="<?php echo imgfolder('out-of-stock.png'); ?>" alt="Out of Stock">
                                        </div>
                                    <?php endif; ?>
                                    <div >
                                        <img src="<?php echo $img; ?>" alt="<?php echo get_the_title(); ?>">
                                        <a href="?add-to-cart=<?php echo get_the_ID(); ?>" data-quantity="1"  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 ></i>Add to cart</a>
                                    </div>
                                    <div >
                                        <a href="<?php echo get_permalink(); ?>">
                                        <h5><?php echo get_the_title(); ?></h5>
                                        <div >
                                            <?php if( $product->get_sale_price() ) : ?>
                                                            <div >
                                                                Cash - <span>P <?php echo wc_price( wc_get_price_to_display( $product, array( 'price' => $product->get_sale_price() ) ) ); ?> </span>
                                                            </div>
                                                            <div >
                                                                <?php echo  'P '.wc_price( wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) ) ); ?>
                                                            </div>
                                                        <?php else : ?>
                                                            <div >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 >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) ?>" >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. */

CodePudding user response:

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; 

                     $stock_quantity = get_post_meta($post->ID, '_stock', true);
                    ?>
                    <?php if ($stock_quantity > 0) : ?>
                        <div >
                            <img src="<?php echo imgfolder('out-of-stock.png'); ?>" alt="Out of Stock">
                        </div>
                    <?php endif; ?>

CodePudding user response:

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;
}

CodePudding user response:

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 >
    <div >
        <div >
            <div >
                <div >
                    <?php if( !$product->is_in_stock() ) : ?>
                <div >
                    <img src="<?php echo imgfolder('out-of-stock.png'); ?>" alt="Out of Stock">
                </div>
            <?php endif; ?>
                    <div >
                        <div >
                            <div  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  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 >
                        <img src="<?php echo $img; ?>" alt="<?php echo get_the_title(); ?>">
                    </div>
                </div>
            </div>
            <div >
                <div >
                    <h3><?php echo get_the_title(); ?></h3>
                    <div >
                        <div >
                            <?php if( $product->get_sale_price() ) : ?>
                                        <div >
                                            Listed Price - <span>P <?php echo wc_price( wc_get_price_to_display( $product, array( 'price' => $product->get_sale_price() ) ) ); ?> </span>
                                        </div>
                                        <div >
                                            <?php echo  'P '.wc_price( wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) ) ); ?>
                                        </div>
                                    <?php else : ?>
                                        <div >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 >Terms - <span>P <?php echo get_field('terms'); ?></span></div>
                        <?php endif; ?>
                         <?php if(get_field('dp_and_pay')): ?>
                            <div ><?php echo get_field('dp_and_pay'); ?></div>
                        <?php endif; ?>
                    </div>
                    <div >
                        <?php echo apply_filters( 'the_content', $product->get_short_description() ); ?>
                    </div>
                    <a href="?add-to-cart=<?php echo get_the_ID(); ?>" data-quantity="1"  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 ></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 >
        <div >
            <div >
                <div >
                    <div >
                        <?php $salehead = get_field('new_arrivals_content',8); ?>
                        <h3>Recommmended Items</h3>
                        <div ><?php echo $salehead['content']; ?></div>
                    </div>
                </div>
            </div>
            <div >
                <div >
                    <div >
                        <?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 >
                                    <?php if( !$new_product->is_in_stock() ) : ?>
                                        <div >
                                            <img src="<?php echo imgfolder('out-of-stock.png'); ?>" alt="Out of Stock">
                                        </div>
                                    <?php endif; ?>
                                    <div >
                                        <img src="<?php echo $img; ?>" alt="<?php echo get_the_title(); ?>">
                                        <a href="?add-to-cart=<?php echo get_the_ID(); ?>" data-quantity="1"  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 ></i>Add to cart</a>
                                    </div>
                                    <div >
                                        <a href="<?php echo get_permalink(); ?>">
                                        <h5><?php echo get_the_title(); ?></h5>
                                        <div >
                                            <?php if( $new_product->get_sale_price() ) : ?>
                                                            <div >
                                                                Cash - <span>P <?php echo wc_price( wc_get_price_to_display( $new_product, array( 'price' => $new_product->get_sale_price() ) ) ); ?> </span>
                                                            </div>
                                                            <div >
                                                                <?php echo  'P '.wc_price( wc_get_price_to_display( $new_product, array( 'price' => $new_product->get_regular_price() ) ) ); ?>
                                                            </div>
                                                        <?php else : ?>
                                                            <div >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 >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) ?>" >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' );
  • Related