Woocommerce Product Quantity return error when use in function.php

I use this get_stock_quantity(); ?> this to get woo-commerce product quantity post loop and its works, but when I use this in function.php in a function loop then its not work. this return a php fatal error.

Error like this: PHP Fatal error: Call to a member function get_stock_quantity() on null in /home/username/public_html/wp-content/themes/etrostore-child-theme/load-more-posts.php on line 142

So the get_stock_quantity(); ?> code works in page template. But not work in function.php

Here is my function.php code:

function filter_post_ajax(){
    global $product;
    $noffset = $_POST["noffset"];
    $nppp = $_POST["nppp"];
    $cat_id = $_POST[ 'category' ];
    $args = array(
        'post_type' => 'product',
        'post_status' => 'publish',
        //'cat' => $cat_id,
        'tax_query' => array(
                    'taxonomy' => 'product_cat',
                    'field' => 'term_id',
                    'terms' => $cat_id,
                    'operator'      => 'IN'
        'orderby' => 'title',
        'order' => 'ASC',
        'posts_per_page' => $nppp,
        'offset' => $noffset,

    $loop = new WP_Query($args);
    while ($loop->have_posts()) : $loop->the_post(); ?>
    <div class="col-xs-6 col-sm-4 col-md-3 product-cols">
        <div class="product-image">
            <a href="<?php  the_permalink()?>" >
                <?php the_post_thumbnail( 'medium' ); ?>
            <div class="overlay">
                <div class="overly_cart_details animated fadeIn">
                    <a href="#">Add to Bag</a>
        <div class="product-title">
            <h3><?php the_title(); ?></h3>
            $saleprice = get_post_meta( get_the_ID(), '_sale_price', true);
            $regularprice = get_post_meta( get_the_ID(), '_regular_price', true);
        <?php if(!(empty($regularprice)) ){ ?>
        <div class="product-price">
            <span class="price">
                <?php if ( $saleprice ) { ?>    
                    ৳ <?php echo BanglaConverter::en2bn($saleprice);  ?>
                <?php } else { ?>
                    ৳ <?php echo BanglaConverter::en2bn($regularprice);  ?>
                <?php } ?>
        <div class="product-add-bag add-to-cart-btn">
            <button class="plusminus">+</button>
            <div class="quantites">
                <input type="number" class="input-text qty text" step="1" min="1" max="<?php echo $product->get_stock_quantity(); ?>" name="quantity" value="<?php if(woo_in_cart($product->id)) {echo woo_in_cart($product->id);}else{echo '1';} ?>" title="Qty" size="4" pattern="[0-9]*" inputmode="numeric">
            <button class="plusminus">-</button>

            <?php woocommerce_template_loop_add_to_cart(); ?>
        <?php } else {?>
        <div class="product-add-bag add-to-cart-btn">
            <a href="<?php the_permalink() ?>"> View Details </a>
        <?php } ?>
    </div> <!-- col-md-4 -->
    <?php endwhile; die();

What’s wrong I’m doing here? Please help me.

Read more here: Woocommerce Product Quantity return error when use in function.php

Leave a Reply

Your email address will not be published. Required fields are marked *