这有点奇怪,但我很难找到如何做到这一点的答案:
This is a bit of an odd one, but I'm having trouble finding an answer for how to do this:
我正在尝试创建一个自定义查询,以获取 WooCommerce 订单中包含的所有产品(及其元数据).
I'm attempting to create a custom query that gets all the products (and their meta) which are contained within a WooCommerce order.
例如,假设我有这些订单待处理:
For example, let's say I have these orders pending:
我的目标是根据这些订购的产品创建一个列表,该列表按产品的元进行排序.像这样:
My goal is to create a list based off these ordered products, which is ordered by the product's meta. Something like this:
但同样,这些只能是订单中的产品(包括数量).
But again, these would only be products (including their quantities) that sit within an order.
我面临的问题是订购商品"和订单项元"存储在数据库中的数据非常有限,并且不会显示与产品相关的所有信息.因此,换句话说,我没有找到一种方法来获取创建循环以创建我的列表所需的信息.
The problem I'm facing is that "Order Items" and "Order Item Meta" as stored in the database are quite limiting and do not display all the information associated with a product. So in other words, I'm not finding a way to get to the info I need to create the loop to create my list.
我的数据库技能有限,因此不胜感激!
My DB skills are limited so ideas would be appreciated!
以下是一些可能对您有所帮助的相关代码:
Here is some related code that might help you:
add_filter( 'woocommerce_shop_order_search_fields', 'woocommerce_shop_order_search_product_cat' );
function woocommerce_shop_order_search_product_cat( $search_fields ) {
$args = array(
'post_type' => 'shop_order'
);
$orders = new WP_Query($args);
if($orders->have_posts()) {
while($orders->have_posts()) {
$post = $orders->the_post();
$order_id = get_the_ID();
$order = new WC_Order($order_id);
$items = $order->get_items();
foreach($items as $item) {
$product_cats = wp_get_post_terms( $item['product_id'], 'product_cat' );
foreach($product_cats as $product_cat) {
add_post_meta($order_id, "_product_cat", $product_cat->name);
}
}
}
};
$search_fields[] = '_product_cat';
return $search_fields;
}
这篇关于WooCommerce 按产品元查询订单项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!