在function.php中使用时Woocommerce Product Quantity返回错误

时间:2017-05-05 08:17:33

标签: php wordpress

我使用这个<?php echo $product->get_stock_quantity(); ?>来获取woo-commerce产品数量后循环及其工作原理,但是当我在函数循环中的function.php中使用它时它不起作用。这会返回一个php致命的错误。

错误如下: 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

因此<?php echo $product->get_stock_quantity(); ?>代码适用于页面模板。但不能在function.php中工作

这是我的function.php代码:

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(
                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' ); ?>
            </a> 
            <div class="overlay">
                <div class="overly_cart_details animated fadeIn">
                    <a href="#">Add to Bag</a>
                </div>
            </div>
        </div>
        <div class="product-title">
            <h3><?php the_title(); ?></h3>
        </div>
        <?php
            $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 ) { ?>    
                    &#x9f3; <?php echo BanglaConverter::en2bn($saleprice);  ?>
                <?php } else { ?>
                    &#x9f3; <?php echo BanglaConverter::en2bn($regularprice);  ?>
                <?php } ?>
            </span>
        </div>
        <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">
            </div>
            <button class="plusminus">-</button>

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

我在这里做错了什么?请帮帮我。

1 个答案:

答案 0 :(得分:1)

您已在循环之外宣布全球$ product ,因此未将 $ product 作为对象 >。

while ($loop->have_posts()) : $loop->the_post();

   $p=wc_get_product(get_the_ID()); //it will return the current product object
   echo $p->get_stock_quantity();     
   /* your other codes */

endwhile;

希望这会对你有所帮助......