Woocommerce 循环中每个订单项目的总计数 [英] Total count for each order item in a loop on Woocommerce
问题描述
我正在同时学习 PHP 和 Woocommerce!我正在尝试获取所有有状态处理的订单和每个订单的总数,并将其显示在页面上.
Hi I'm learning PHP and Woocommerce at the same time! I'm trying to get all the orders which have status processing and the total count for each and display this on a page.
到目前为止,我可以遍历所有订单并获取每个订单的名称和数量.
Thus far I can loop through all the orders and get the name and quantity of each.
但由于我不知道哪个产品会出现在列表中,我不确定如何比较名称然后添加数量.
but as I don't know which product is going to be in the list, I'm not sure how I would compare the name and then add the quantity.
我目前的输出是这样的:
My current output is like this:
prod1 - v1 x 1
prod1 - v1 x 1
Prod2 - v3 x 1
Prod2 - v3 x 1
prod2 - v3 x 1
prod2 - v3 x 1
prod3 - v2 x 11
prod3 - v2 x 11
prod3 - v2 x 1
prod3 - v2 x 1
我想要的是:
prod1 - v1 x 1
prod1 - v1 x 1
Prod2 - v3 x 2
Prod2 - v3 x 2
prod3 - v2 x 12
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
Class(SQL 查询):
Instead of using a WP_Query
and some heavy code to get your calculations, you should better use this much more lighter and effective version code, using WPDB
Class (a SQL query):
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>';
}
产品数量将仅基于处理订单状态,而不是产品总销售额.
The product count will be based only on the processing oder status and not on product total sales.
经过测试和工作.
改为获取产品变体 (如您的评论中所问)
如您在评论中要求获取订单中的产品变体,您将替换该行:
As asked in your comment to get the product variations that are in the order, you will replace the line:
AND woim2.meta_key LIKE '_product_id'
通过以下行:
AND woim2.meta_key LIKE '_variation_id'
<小时>
获取所有产品和变体(不包括可变产品)
要获取包括产品变体但不包括可变产品的所有产品,请使用:
To get all products including Product Variations but excluding Variable Products use:
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>';
}
经过测试和工作.
这篇关于Woocommerce 循环中每个订单项目的总计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!