2017-05-05 136 views
1

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

声明该全球$产品,所以它没有采取$产品对象

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; 

希望这将帮助你......

+0

作品!非常感谢。 :) –