php - 从 WooCommerce 订单中按产品类别订购的产品数量总和
问题描述
我真的坚持这一点,我非常感谢任何帮助。
目标是计算 Woocommerce 订单上每个类别中的商品数量,以便每个部分都可以以类别名称和产品数量为开头。例如:
汉堡 x 5
下面将是该订单上所有汉堡的列表。
现在我以为我可以使用以下代码:
$categories = [];
foreach ($order->get_items() as $item) {
$product_id = $item['product_id'];
$meta = $item['item_meta'];
$meta = array_filter($meta, function ($key) {
return !in_array($key, Order::getHiddenKeys());
}, ARRAY_FILTER_USE_KEY);
$terms = get_the_terms($product_id, 'product_cat');
$cat_id = $terms[0]->term_id;
$categories[$cat_id][] = $item;
$cat_name = $terms[0]->name;
}
foreach($categories as $category => $items){
?>
<tr>
<td colspan="3">
<h4>
<?php
if( $term = get_term_by( 'id', $category, 'product_cat' ) ){
echo $term->name.' ('.$term->count.')';
echo " × ";
echo count($items);
}
?>
</h4>
</td>
</tr>
<?php
但我意识到这只是计算该类别中已订购的产品数量,而不是数量。因此,如果其中一个有多个相同的产品,则不会计算在内。例如,如果类别包含以下三个项目:
芝士汉堡 x 2
汉堡 x 1
将返回两个而不是三个。
我一直在查看类别和术语数组,但似乎找不到任何可行的方法。我确定我错过了一些东西,但我现在只是不知所措。
谁能指出我正确的方向?
(另外,如果它有任何用处,这是上一个问题Filtering the Woocommerce $order items By Category (Term)的间接后续)
解决方案
您可以通过两种方式做到这一点:
- 根据从产品中获得的产品类别将订购的产品数量相加(因此必须只有1)
- 设置您想要获得购买产品数量计数的特定产品类别列表
解决方案#1
您可以使用该get_the_terms()
功能获取产品类别。
函数参数必须是要统计的订单的ID。
请注意,只会get_the_terms()
检查该函数返回的第一个产品类别。如果一个产品有多个,你可能会得到意想不到的结果。
// gets an array with the quantities of the ordered products grouped by product categories
function gets_the_quantities_of_products_ordered_grouped_by_product_categories( $order_id ) {
// initializes the array that will contain the product categories with the ordered quantities of each
$count_by_cat = array();
// get the order object
$order = wc_get_order( $order_id );
// for each product ordered
foreach ( $order->get_items() as $item ) {
$product = $item->get_product();
// get product categories
$terms = get_the_terms( $product->get_id(), 'product_cat' );
foreach ( $terms as $term ) {
$cat_name = $term->name;
// if the product category is already present in the array, add the quantities
if ( array_key_exists( $cat_name, $count_by_cat ) ) {
$count_by_cat[$cat_name] += $item->get_quantity();
// otherwise it adds the category and quantity to the array
} else {
$count_by_cat[$cat_name] = $item->get_quantity();
}
break;
}
}
return $count_by_cat;
}
解决方案#2
在这种情况下,您必须使用要获取订购产品数量的产品类别名称列表来初始化一个数组。
使用该has_term()
功能,您可以检查产品是否属于特定产品类别。
函数参数必须是要统计的订单的ID。
// gets an array with the quantities of the ordered products grouped by product categories
function gets_the_quantities_of_products_ordered_grouped_by_product_categories( $order_id ) {
// initializes the array that will contain the product categories with the ordered quantities of each
$count_by_cat = array();
// set the product categories you want to get counts for
$categories = array(
'Clothes',
'Shoes',
'Pants',
'Jackets',
'Hats'
);
// get the order object
$order = wc_get_order( $order_id );
// for each product ordered
foreach ( $order->get_items() as $item ) {
$product = $item->get_product();
// for each product category
foreach ( $categories as $cat_name ) {
// check if the product belongs to one of the product categories
if ( has_term( $cat_name, 'product_cat', $product->get_id() ) ) {
// if the product category is already present in the array, add the quantities
if ( array_key_exists( $cat_name, $count_by_cat ) ) {
$count_by_cat[$cat_name] += $item->get_quantity();
// otherwise it adds the category and quantity to the array
} else {
$count_by_cat[$cat_name] = $item->get_quantity();
}
break;
}
}
}
return $count_by_cat;
}
这两个函数都将返回一个类似于此的数组:
$count_by_cat = array(
'Shoes' => 4,
'Pants' => 2,
'Hats' => 11
)
因此,您可以打印类似于以下内容的 HTML 代码:
// print the counts for each product category
foreach ( $count_by_cat as $category_name => $count ) {
echo "<p><strong>" . $category_name . ":</strong> " . $count . "</p>";
}
两种代码都已经过测试并且可以工作。将它们添加到您的活动主题的functions.php。
推荐阅读
- session - 令牌身份验证是否使会话变得不必要?
- javascript - 在服务器端检测客户端是否拒绝我的 TLS 证书
- java - 扩展 JBoss Data Virt ws 转换器以处理 JSONP
- scala - 获取 Scala 集合变量的类型
- django - 如何对与相应模型不同的数据库进行初始迁移?
- webpack - 模块构建失败(来自 ./node_modules/babel-loader/lib/index.js):TypeError:无法读取 null 的属性“绑定”
- php - 为什么我的包含与不正确的 DIRECTORY_SEPARATOR 一起工作?
- javascript - 如何将单击事件绑定到已动态呈现的添加的 select2 下拉列表
- python - 使用python绘制/绘制excel文件数据
- php - 包含和输出另一个 PHP 变量的 PHP 字符串