【问题标题】:how i can retrieve all woocommerce orders in new custom plugin我如何在新的自定义插件中检索所有 woocommerce 订单
【发布时间】:2021-01-17 21:02:27
【问题描述】:

我正在创建一个新的自定义插件,需要检索所有 woocommerce 订单

我尝试使用:wc_get_orders() 函数来检索所有 woocommerce 订单,但我得到了

Uncaught Error: Call to undefined function wc_get_orders()

然后这样做了:

require_once '../woocommerce/includes/wc-order-functions.php'; 

但我得到了:

require_once(../woocommerce/includes/wc-order-functions.php): failed to open stream: 

我如何检索所有 woocommerce 订单

【问题讨论】:

  • 看看:github.com/woocommerce/woocommerce/wiki/…提示:“函数 wc_get_orders() 的参数太少,传入 0,预期正好 1”。 另一个提示: $orders = wc_get_orders( array( 'customvar' => 'somevalue' ) );
  • 感谢我尝试使用的助手并收到此错误 $query = new WC_Order_Query( array( 'limit' => 10, 'orderby' => 'date', 'order' => 'DESC ', 'return' => 'ids', ) ); $orders = $query->get_orders();未捕获的错误:未找到“WC_Order_Query”类...我应该做任何包含或使用吗?
  • 代码本身可以工作,但是,您的问题中缺少的信息是您应用此代码的位置和方式。如果您将代码添加到functions.php,它应该可以正常工作。所以看来问题不在于代码,而在于它的使用
  • 我正在创建一个自定义插件,并且需要列出所有订单

标签: php wordpress woocommerce plugins


【解决方案1】:

WooCommerce 已经提供 API 来获取所有订单https://example.com/wp-json/wc/v3/orders

更多详情请查看documentation


如果您想要基于函数而不是 REST API,请这样做:

您应该将您的订单函数调用到操作挂钩woocommerce_after_register_post_type 中才能正常工作。所以请像这样附上你的函数调用:

add_action(
    'woocommerce_after_register_post_type', 
    function() {
        $orders = wc_get_orders( array( 'numberposts' => -1 ) );
        var_dump( $orders );
    }
);

然后您可以遍历对象并获取所有必要的详细信息,例如

foreach ( $orders->get_items() as $item_id => $item ) {
   $product_id = $item->get_product_id();
   $variation_id = $item->get_variation_id();
   $product = $item->get_product();
   $name = $item->get_name();
   $quantity = $item->get_quantity();
   $subtotal = $item->get_subtotal();
   $total = $item->get_total();
   $tax = $item->get_subtotal_tax();
   $taxclass = $item->get_tax_class();
   $taxstat = $item->get_tax_status();
   $allmeta = $item->get_meta_data();
   $somemeta = $item->get_meta( '_whatever', true );
   $type = $item->get_type();
}

更多详情请查看article

【讨论】:

  • 我收到了对未定义函数 wc_get_orders() 的调用
  • 我需要在自定义插件中列出订单——我应该包括一些东西?
  • 请分享您的 WooCommerce 版本
  • 版本为4.5.2
  • 您必须在动作挂钩woocommerce_after_register_post_type 中调用您的函数,请查看更新后的答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-05-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-02
  • 2018-04-16
  • 1970-01-01
相关资源
最近更新 更多