wordpress - woocommerce:如何访问数据库中的订单购买
问题描述
我正在尝试从我的 wordpress 管理插件访问用户购买信息,以便我可以总结订单,例如:
- 产品名称
- 自定义数据(在我的例子中是与产品相关的文本)
- 买的时候
- 他们付了多少钱
它主要是用户登录时在“帐户”>“订单”页面上可以找到的信息。
我查看了 woocommerce 表,但找不到此信息。
你能建议我可以查询哪些表来汇总我在上面寻找的信息吗?
解决方案
订单以“shop_order”自定义帖子类型存储在wp_posts表中。您可以像这样简单地使用 woocommerce 功能。
$woo_orders = wc_get_orders( array('numberposts' => -1) );
/* Loop each WC_Order object */
foreach( $woo_orders $order ){
/* Get the ID */
echo $order->get_id();
/* Get the status */
echo $order->get_status(); // The status
}
或者使用普通的 wordpress 循环:
$loop = new WP_Query( array(
'post_type' => 'shop_order',
'posts_per_page' => -1,
'post_status' => 'wc-ywraq-new' //will get the new order
) );
// Your post loop
if ( $loop->have_posts() ):
while ( $loop->have_posts() ) : $loop->the_post();
// The ID
$order_id = $loop->post->ID;
// The object from WC_Order find the reference in woocommerce docs
$order = wc_get_order($loop->post->ID);
endwhile;
wp_reset_postdata(); // always
endif;
这是来自 github 的参考:https ://github.com/woocommerce/woocommerce/wiki/wc_get_orders-and-WC_Order_Query
推荐阅读
- r - 每日时间序列分析(周一至周五)
- windows - 通过批处理脚本中的参数调用等待微调器
- kubernetes - 是否可以在同一个路由映射中使用 Ambassador prefix_regex 和 regex_rewrite?
- flutter - flutter : 如何在颤动中更改所选文本的背景颜色
- swift - 带有嵌套枚举的 Swift 枚举,如何从 RawRepresentable 协议的字符串中实现关联值
- r - 我应该如何找到 R 中多个变量之间的相关性?“标准差为 0”错误
- php - 什么是 Cookie“pma_lang_https”?
- php - 注意尝试访问 null wordpress 插件类型值的数组偏移量
- r - 如何在ggplot2中将y轴标题(不是标签)旋转为水平
- swift - 按钮项目保存和按钮项目取消在具有核心数据的应用程序中不起作用