gpt4 book ai didi

php - 通过变体 ID 获取 WooCommerce 订单项目

转载 作者:可可西里 更新时间:2023-10-31 23:15:55 25 4
gpt4 key购买 nike

如何根据特定产品变体列出订单商品?

给定一个变体 ID,我想生成一个包含该特定变体的所有订单商品的列表。

在我的例子中,我使用变体来表示日期,而产品本身是一个周期性事件,因此这样做的目的是列出参加该特定日期的人员。

如果这可以通过使用 meta_keys 的简单操作(例如 get_posts 或类似的东西)来实现,那就太棒了,否则我猜自定义查询会成为方式。

我似乎无法弄清楚这些表在这种情况下是如何关联的,或者它是否以可搜索的方式存储。

非常感谢任何帮助。

谢谢!

最佳答案

有很多方法可以做到这一点。在这里,我在自定义函数中使用了一个 SQL 查询和一个 $variation_id (要设置的变体 ID) 作为其中的参数:

function get_all_orders_items_from_a_product_variation( $variation_id ){

global $wpdb;

// Getting all Order Items with that variation ID
$item_ids_arr = $wpdb->get_col( $wpdb->prepare( "
SELECT `order_item_id`
FROM {$wpdb->prefix}woocommerce_order_itemmeta
WHERE meta_key LIKE '_variation_id'
AND meta_value = %s
", $variation_id ) );

return $item_ids_arr; // return the array of orders items ids

}

代码位于您的事件子主题(或主题)的 function.php 文件中或任何插件文件中。

用法 (此处以变体 ID 41 为例):

这将显示此变体 ID 的订单商品 ID 列表以及一些数据(例如)。

$items_ids = get_all_orders_items_from_a_product_variation( 41 );

// Iterating through each order item
foreach( $items_ids as $item_id ){

// Getting some data (the color value here)
$item_color = wc_get_order_item_meta( $item_id, 'pa_color', true );

// Displaying some data related to the current order item
echo 'Item ID: '. $item_id . ' with color "' . $item_color .'"<br>';
}

此代码已经过测试并且可以工作。


GETTING THE ORDER ID (UPDATED)

现在,如果您需要获取所有订单 ID,您可以通过这种方式在该函数内使用另一个查询:

function get_all_orders_that_have_a_product_variation( $variation_id ){

global $wpdb;

// Getting all Order IDs with that variation ID
$order_ids_arr = $wpdb->get_col( $wpdb->prepare( "
SELECT DISTINCT items.order_id
FROM {$wpdb->prefix}woocommerce_order_items AS items
LEFT JOIN {$wpdb->prefix}woocommerce_order_itemmeta AS itemmeta ON items.order_item_id = itemmeta.order_item_id
WHERE meta_key LIKE '_variation_id'
AND meta_value = %s
", $variation_id ) );

return $order_ids_arr; // return the array of orders ids

}

代码位于您的事件子主题(或主题)的 function.php 文件中或任何插件文件中。

用法 (此处始终以变体 ID 41 为例):

这将显示此变体 ID 的订单 ID 列表及其状态(例如)。

$orders_ids = get_all_orders_that_have_a_product_variation( 41 );

// Iterating through each order item
foreach( $orders_ids as $order_id ){

// Getting an instance of the order object
$order = wc_get_order($order_id);

// Displaying some data related to the current order
echo 'Order #'. $order_id . ' has status "' . $order->get_status() .'"<br>';
}

此代码已经过测试并且可以工作。

You can also combine in a more complex array, the Order ID with it's related Items IDs in a multi-dimensional array this way:

function get_all_orders_and_item_ids_that_have_a_product_variation( $variation_id ){

global $wpdb;

// Getting all Order IDs and item ids with that variation ID
$results = $wpdb->get_results( $wpdb->prepare( "
SELECT items.order_id, items.order_item_id AS item_id
FROM {$wpdb->prefix}woocommerce_order_items AS items
LEFT JOIN {$wpdb->prefix}woocommerce_order_itemmeta AS itemmeta ON items.order_item_id = itemmeta.order_item_id
WHERE meta_key LIKE '_variation_id'
AND meta_value = %s
", $variation_id ), ARRAY_A );

return $results; // return a multi-dimensional array of orders Ids / items Ids

}

关于php - 通过变体 ID 获取 WooCommerce 订单项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42527147/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com