php - 在 function.php 中使用时,Woocommerce 产品数量返回错误

标签 php wordpress

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

这样的错误: 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();
}

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

最佳答案

您已在循环之外声明了global $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;

希望这对您有帮助...

关于php - 在 function.php 中使用时,Woocommerce 产品数量返回错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43799902/

相关文章:

css - 将 wordpress 帖子拆分为多个页面(CSS 样式)

php - 从查询中排除当前帖子的缩略图

php - 在 WooCommerce 中更新自定义订单项元

php - WordPress 网站加载时显示错误

php - 如何在平面索引数组中复制值? (按顺序将所有元素追加到数组末尾)

php - 使用带有 net_ssh 的 phpseclib 如何使用 $ssh->exec su 到 root

php - jquery加载不读取字符串作为变量(php混合)

php - 在客户端使用网络摄像头录制视频并上传到php服务器

javascript - 尝试从 AJAX 解析 JSON 数组时出现意外标记 u

javascript - 将参数附加到特定的外部链接