Woocommerce循环中每个订单项目的总计数



嗨,我正在同时学习PHP和Wooccommerce!我正在尝试获取所有进行状态处理的订单以及每个订单的总计数,并将其显示在页面上。

到目前为止,我可以循环浏览所有订单,并获得每个订单的名称和数量。

但由于我不知道清单上会有哪种产品,我不知道该如何比较名称,然后再加上数量。

我当前的输出是这样的:

  • prod1-v1 x 1

  • 产品2-v3 x 1

  • prod2-v3 x 1

  • prod3-v2 x 11

  • prod3-v2 x 1

我想要的是:

  • prod1-v1 x 1

  • 产品2-v3 x 2

  • prod3-v2 x 12

当前代码为:

<?php
/*
Template Name: Print Supplier Order
*/
if (!is_user_logged_in() || !current_user_can('manage_options')) wp_die('This page is private.');
?>
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title><?php _e('Processing Orders'); ?></title>
<style>
body { background:white; color:black; width: 95%; margin: 0 auto; }
</style>
</head>
<body>
<header>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<h1 class="title"><?php the_title(); ?></h1>
<?php the_content(); ?>
<?php endwhile; endif; ?>
</header>
<section>
<?php 
global $woocommerce;
$args = array( 'post_type' => 'shop_order', 'post_status' => 'wc-processing', 'posts_per_page' => -1 );

$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$order_id = $loop->post->ID;
$order = new WC_Order($order_id);
$product_details = array();
$order_items = $order->get_items();
foreach( $order_items as $product ) {
echo $product['name']." x ".$product['qty'];
echo '<br>';
}
?>
<?php endwhile; ?>
</section>
</body>
</html>

已更新 (现在SQL查询还提供了产品名称,使其更轻(

与其使用WP_Query和一些繁重的代码来进行计算,不如使用WPDB类(SQL查询(:来更好地使用这个更轻、更有效的版本代码

global $wpdb;
$results = $wpdb->get_results( "
SELECT DISTINCT woim2.meta_value as id, SUM(woim.meta_value) as count, woi.order_item_name as name
FROM {$wpdb->prefix}woocommerce_order_itemmeta as woim
INNER JOIN {$wpdb->prefix}woocommerce_order_items as woi ON woi.order_item_id = woim.order_item_id
INNER JOIN {$wpdb->prefix}woocommerce_order_itemmeta as woim2 ON woi.order_item_id = woim2.order_item_id
INNER JOIN {$wpdb->prefix}posts as p ON p.ID = woi.order_id
WHERE p.post_status IN ('wc-processing','wc-on-hold')
AND woim.meta_key LIKE '_qty'
AND woim2.meta_key LIKE '_product_id'
GROUP BY woim2.meta_value
" );
foreach( $results as $result ){
echo $result->name . ' (' . $result->id . ') x ' . $result->count . '<br>';
}

产品计数将仅基于处理订单状态,而不基于产品总销售额。

测试并工作。


获取产品变体(如您的评论所述(

正如您在评论中所要求的那样,要获得订单中的产品变体,您将替换以下行:

AND woim2.meta_key LIKE '_product_id'

通过以下线路:

AND woim2.meta_key LIKE '_variation_id'

获取所有产品和变体(不包括可变产品(

要获取包括产品变体但不包括可变产品的所有产品,请使用:

global $wpdb;
$results = $wpdb->get_results( "
SELECT DISTINCT woim2.meta_value as id, SUM(woim.meta_value) as count, woi.order_item_name as name
FROM {$wpdb->prefix}woocommerce_order_itemmeta as woim
INNER JOIN {$wpdb->prefix}woocommerce_order_items as woi ON woi.order_item_id = woim.order_item_id
INNER JOIN {$wpdb->prefix}woocommerce_order_itemmeta as woim2 ON woi.order_item_id = woim2.order_item_id
INNER JOIN {$wpdb->prefix}posts as p ON p.ID = woi.order_id
WHERE p.post_status IN ('wc-processing','wc-on-hold') 
AND woim.meta_key LIKE '_qty'
AND ((woim2.meta_key LIKE '_variation_id' AND woim2.meta_value > 0)
OR (woim2.meta_key LIKE '_product_id'
AND woim2.meta_value NOT IN (SELECT DISTINCT post_parent FROM {$wpdb->prefix}posts WHERE post_type LIKE 'product_variation')))
GROUP BY woim2.meta_value
" );
foreach( $results as $result ){
echo $result->name . ' (' . $result->id . ') x ' . $result->count . '<br>';
}

测试并工作。


获取所有(甚至产品变体和可变产品(

global $wpdb;
$results = $wpdb->get_results( "
SELECT DISTINCT woim2.meta_value as id, SUM(woim.meta_value) as count, woi.order_item_name as name
FROM {$wpdb->prefix}woocommerce_order_itemmeta as woim
INNER JOIN {$wpdb->prefix}woocommerce_order_items as woi ON woi.order_item_id = woim.order_item_id
INNER JOIN {$wpdb->prefix}woocommerce_order_itemmeta as woim2 ON woi.order_item_id = woim2.order_item_id
INNER JOIN {$wpdb->prefix}posts as p ON p.ID = woi.order_id
WHERE p.post_status IN ('wc-processing','wc-on-hold')
AND woim.meta_key LIKE '_qty'
AND ((woim2.meta_key LIKE '_variation_id' AND woim2.meta_value > 0)
OR woim2.meta_key LIKE '_product_id' )
GROUP BY woim2.meta_value
" );
foreach( $results as $result ){
echo $result->name . ' (' . $result->id . ') x ' . $result->count . '<br>';
}

测试并工作。

方法1

有几种不同的方法可以解决这个问题,但如果我们只想稍微调整您的代码,那么我们可以通过将产品循环中的产品添加到自定义数组中,使用产品名称作为键,然后处理该数组来实现这一点。

这里有一个例子:

global $woocommerce;
//custom array to list products:
$products_array = array();
//start wp query
$args = array( 
'post_type'     => 'shop_order', 
'post_status'   => 'wc-processing', 
'posts_per_page'=> -1 
);
$loop = new WP_Query( $args );
//loop through orders
while ( $loop->have_posts() ) {
$loop->the_post();
//vars
$order_id           = $loop->post->ID;
$order              = new WC_Order($order_id);
$product_details    = array();
$order_items        = $order->get_items();
//loop through products
foreach( $order_items as $product ) {
//if product has been captured to custom array before, increment value, else add it.
if (array_key_exists($product['name'], $products_array)) {
$products_array[$product['name']] += $product['qty'];
} else {
$products_array[$product['name']] = $product['qty'];
}
}
}
//end wp_query
wp_reset_postdata();
//display list:
foreach ($products_array as $title => $quantity) {
echo $title.' x '.$quantity;
}

方法2

循环浏览所有产品,而不是所有订单(过一段时间,假设商店成功了,订单可能会比产品多得多,所以从长远来看,这种方法可能会更快(。每种产品都将其总销售额存储为元值,因此访问它非常简单:

//WP Query args
$args = array(
'post_type'     => 'product',
'posts_per_page'=> -1,
'meta_key'      => 'total_sales',
'orderby'       => 'title',
'order'         => 'ASC',
'meta_query'    => array( //make sure we only grab items that have actually sold
array(
'key'       => 'total_sales',
'value'     => 0,
'compare'   => '>'
)
)
);
$query = new WP_Query($args);
//loop through products
if($query->have_posts()) {
while($query->have_posts()) {
$query->the_post();
//use built in Wordpress functions for get_the_title() and get_post_meta()
echo get_the_title().' x '.get_post_meta(get_the_ID(), 'total_sales', true);
}
}
//end wp query
wp_reset_postdata();

最新更新