douhuijun3776 2018-09-26 08:54
浏览 139
已采纳

无法获得库存和缺货WC_Product_Query Woocommerce的产品

I'm using new WC_Product_Query to get products for a stock report but so far I'm only able to set In-stock or Out-of-Stock in the query.

How do I go about setting the query to get all products regardless of stock status or set it to just grab In-stock and Out-of-Stock products as we don't use backorders?

$query = new WC_Product_Query( array(
                        'orderby' => 'title',
                        'order' => 'ASC',
                        'return' => 'ids',
                        'category' => $club_cat,
                        'limit' => -1,
                    ) );

Its probably worth mentioning that if i use 'stock_status' => 'outofstock', I don't get any products returned and 'stock_status' => 'instock', only returns products that are in stock.

I'm not sure if it's relevant to this but we use a mix of simple and variable products some are stock managed at the product level and others stock managed at the variation level.

Thanks for reading :)

  • 写回答

1条回答 默认 最新

  • dongwu1992 2018-09-26 10:02
    关注

    Your code works just perfectly. I have tested this query:

    $query = new WC_Product_Query( array(
        'orderby' => 'title',
        'order' => 'ASC',
        //'return' => 'ids',
        'limit' => -1,
        'stock_status' => 'outofstock',
    ) );
    

    Important note: The WC_Product_Query doesn't loop Product variations, as it's made for "product" post type only.

    So you should better use a WP_Query instead even if database changes are planed in future WooCommerce versions and that will not happen for a while anyways. WC_Product_Query and WC_Order_Query still need very big enhancements as they are very limited for instance.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化