admin管理员组文章数量:1425802
I have a custom admin page that needs to search through all shop orders to see if they contain a specific item. The relevant code is this:
$args = array(
'post_status' => 'any',
'post_type' => 'shop_order',
'posts_per_page' => -1
);
$order_query = new WP_Query($args);
if ($order_query->have_posts()) {
while ($order_query->have_posts()) : $order_query->the_post();
global $post;
$order = wc_get_order($post->ID);
$items = $order->get_items();
foreach($items as $item) {
// Check if item data matches what was searched for
if($item['variation_id'] == $product_variation_ID && $item['start-date'] == $start_date && $item['location'] == $location) {
// This order contains the item we're looking for
}
}
endwhile;
}
wp_reset_postdata();
Basically I get all shop orders, call wc_get_order()
on each shop order ID, and then call get_items()
on each order object. I then loop through the items to see if each item matches what was searched for. Each order only contains a single item, although that shouldn't change anything.
This code works fine, except that now that the site has over 1,000 orders, the query is simply too resource intensive on shared hosting, I'm hitting the host's memory limit and it's failing to complete. Specifically, it's failing when calling wc_get_order()
on each shop_order post ID.
Is there any way I can make this query more efficient? I can't think of any other way to get the items from a shop_order post type without calling wc_get_order()
, even though every order only contains a single item. I'd rather not do this over 1,000 times but I just can't see another way.
I may have to move to a VPS to resolve this temporarily, but I feel like eventually I'll run into the same issue even with the additional resources of a VPS, as the number of shop_order posts to loop through is only increasing.
I have a custom admin page that needs to search through all shop orders to see if they contain a specific item. The relevant code is this:
$args = array(
'post_status' => 'any',
'post_type' => 'shop_order',
'posts_per_page' => -1
);
$order_query = new WP_Query($args);
if ($order_query->have_posts()) {
while ($order_query->have_posts()) : $order_query->the_post();
global $post;
$order = wc_get_order($post->ID);
$items = $order->get_items();
foreach($items as $item) {
// Check if item data matches what was searched for
if($item['variation_id'] == $product_variation_ID && $item['start-date'] == $start_date && $item['location'] == $location) {
// This order contains the item we're looking for
}
}
endwhile;
}
wp_reset_postdata();
Basically I get all shop orders, call wc_get_order()
on each shop order ID, and then call get_items()
on each order object. I then loop through the items to see if each item matches what was searched for. Each order only contains a single item, although that shouldn't change anything.
This code works fine, except that now that the site has over 1,000 orders, the query is simply too resource intensive on shared hosting, I'm hitting the host's memory limit and it's failing to complete. Specifically, it's failing when calling wc_get_order()
on each shop_order post ID.
Is there any way I can make this query more efficient? I can't think of any other way to get the items from a shop_order post type without calling wc_get_order()
, even though every order only contains a single item. I'd rather not do this over 1,000 times but I just can't see another way.
I may have to move to a VPS to resolve this temporarily, but I feel like eventually I'll run into the same issue even with the additional resources of a VPS, as the number of shop_order posts to loop through is only increasing.
Share Improve this question asked Jun 29, 2017 at 7:18 Josh WarrenJosh Warren 1892 silver badges9 bronze badges1 Answer
Reset to default 1I'm doing something similar. I started off with an approach similar to yours, but made it slightly faster by directly using WC API Query instead of WP Query.
I am still looking for a way to query the variation from all orders...
//get number of orders per variation_id
function getOrdersfromVariation($variation_id){
$numberOfOrders = 0;
ip_write_log("getOrdersfromVariation varid: $variation_id");
// rewrite with wc_get_orders
$args = array(
'status' => array( 'processing', 'completed'),
'limit' => -1,
);
$orders = wc_get_orders( $args );
if(isset($orders)){
//TODO: Get order count - $total_orders = $orders->total;
foreach ($orders as $order){
foreach ($order->get_items() as $key => $lineItem) {
$item_data = $lineItem->get_data();
if ($item_data['variation_id'] == $variation_id) {
$numberOfOrders++;
}
}
}
if(isset($numberOfOrders)){
return $numberOfOrders;
}
}
return;
}
本文标签: Optimizing Woocommerce order items query
版权声明:本文标题:Optimizing Woocommerce order items query 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745451869a2658923.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论