Php Woocommerce wp_查询按ID获取订单
我试图生成订单数据的简单输出。第一步是一个WP_查询(也许),所以我写了这段代码Php Woocommerce wp_查询按ID获取订单,php,wordpress,woocommerce,foreach,orders,Php,Wordpress,Woocommerce,Foreach,Orders,我试图生成订单数据的简单输出。第一步是一个WP_查询(也许),所以我写了这段代码 $args = array ( 'post_type' =>'shop_order', 'posts_per_page' => -1, 'post_status' => 'any', //'p' => $post_id, ); $order_query = new WP_Query( $args ); while ( $order_query
$args = array (
'post_type' =>'shop_order',
'posts_per_page' => -1,
'post_status' => 'any',
//'p' => $post_id,
);
$order_query = new WP_Query( $args );
while ( $order_query->have_posts() ) :
$order_query->the_post();
echo the_ID();
echo ' : ';
the_title();
echo '<br/><br/>';
endwhile;
我认为WP_查询是一种显而易见的方式,但它看起来好像获取商业订单数据一点也不简单。更新2 要获取一个订单的订单数据,您不需要
WP\u query
。您可以直接使用:
$order = wc_get_order( $order_id );
$order->id; // order ID
$order->post_title; // order Title
$order->post_status; // order Status
// getting order items
foreach($order->get_items() as $item_id => $item_values){
// Getting the product ID
$product_id = $item_values['product_id'];
// .../...
}
更新1 您应该尝试这样做,就像使用数组键(wc\u get\u order\u statuses()一样,您将获得所有订单状态,并且使用
'numberposts'=>-1,
所有现有订单
下面是另一种方法(不使用WP_查询,也可以在WP_查询数组中使用这些参数):
$customer\u orders=get\u posts(数组(
“numberposts”=>-1,
“post_type”=>“shop_order”,
“post_status”=>数组_键(wc_get_order_status())
) );
//检查每个当前客户订单
foreach($customer\u orders作为$customer\u order){
//正在获取订单ID、标题和状态
$order\u id=$customer\u order->id;
$order\u title=$customer\u order->post\u title;
$order\u status=$customer\u order->post\u status;
//显示订单ID、标题和状态
回显“订单ID:”.$Order_ID.
”;
回显“订单标题:”.$Order_title.“
”;
回显“订单状态:”.$Order_状态。“
”;
//获取order对象的实例
$order=wc\u get\u order($order\u id);
//检查每个当前客户订单项目
foreach($order->get\u items()作为$item\u id=>$item\u值){
//获取产品ID
$product_id=$item_值['product_id'];
//显示产品ID
回显“产品ID:”.$Product_ID.”;
}
}
这个问题的严格答案是将'p'=>$post\u id'
更改为'post\u in'=>数组($post\u id)
…但是,如果有人走这条路,真正的答案是解析电子邮件要容易得多,woocommerce已经为您完成了大部分工作。在这一过程中还有其他的失误;1.帖子受到保护,订单注释在摘录中,因此无法显示(我可以将它们移动到meta,但是…)2.订单项目在注释中,必须循环然后解析以产生有意义的输出,因此我不妨直接解析电子邮件。作为背景,我有一个Filemaker数据库,可以解析Romancart购物解决方案中的电子邮件。当然,解析Woocommerce中的订单电子邮件,但我认为我会更聪明一点ss通过网页获取数据。ESS当然是最好的解决方案,但它也是非常密集的编程。这可能有助于'post\u status'=>'any'
实现这一点,返回所有帖子并不是问题。返回一篇帖子是我的目标,请参见上面的答案。但无论如何,感谢您花时间回答。这值得注意,因为移动Wooco向Filemaker发送电子商务数据是人们可能想做的事情,单个Woocommerce订单是相互关联的表格条目的纠结,这一事实意味着任何解决方案都将是复杂的,订单电子邮件可能是订单中完整数据集的最佳顺序摘要。您的问题非常不清楚……我没有理解你试图调用一个订单。你最好直接使用这个$order=wc\u get\u order($order\u id)
…不需要查询…然后如果你打印($order)
你会看到你可以得到所有东西…我已经更新了我的答案两次。
$order = wc_get_order( $order_id );
$order->id; // order ID
$order->post_title; // order Title
$order->post_status; // order Status
// getting order items
foreach($order->get_items() as $item_id => $item_values){
// Getting the product ID
$product_id = $item_values['product_id'];
// .../...
}
$customer_orders = get_posts( array(
'numberposts' => -1,
'post_type' => 'shop_order',
'post_status' => array_keys( wc_get_order_statuses() )
) );
// Going through each current customer orders
foreach ( $customer_orders as $customer_order ) {
// Getting Order ID, title and status
$order_id = $customer_order->ID;
$order_title = $customer_order->post_title;
$order_status = $customer_order->post_status;
// Displaying Order ID, title and status
echo '<p>Order ID : ' . $order_id . '<br>';
echo 'Order title: ' . $order_title . '<br>';
echo 'Order status: ' . $order_status . '<br>';
// Getting an instance of the order object
$order = wc_get_order( $order_id );
// Going through each current customer order items
foreach($order->get_items() as $item_id => $item_values){
// Getting the product ID
$product_id = $item_values['product_id'];
// displaying the product ID
echo '<p>Product ID: '.$product_id.'</p>';
}
}