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

Get WooCommerce orders Items by variation ID

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

给定一个 变体 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 文件或任何插件文件。

USAGE (此处以变体 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 文件或任何插件文件。

USAGE (这里总是以变体 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

}

我尝试做同样的事情,在查看 db 后,我在 '_variation_id' 前面得到 0。但是使用我的自定义 hack 为未来寻找相同问题的人获得了订单项目。我们需要使用 product_namevariaion_name(例如 Size 等)和 variation_value LIKE 'S'、'L' 等进行比较。考虑这个函数:

function get_all_orders_that_have_a_product_variation( $product_name, $variation_name, $variation_value ){

global $wpdb;
// Getting all Order IDs with that variation ID

$order_ids_arr = $wpdb->get_col($wpdb->prepare ( "
        SELECT * FROM wpcc_woocommerce_order_items items 
        LEFT JOIN wpcc_woocommerce_order_itemmeta itemmeta ON items.order_item_id = itemmeta.order_item_id 
        WHERE items.order_item_name LIKE %s 
        AND itemmeta.meta_key LIKE %s 
        AND itemmeta.meta_value = %s", $product_name, $variation_name, $variation_value ));


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

}

现在调用它,假设我们的产品名称是 Raleigh sport,变体名称是 size,值是 s。在简单的形式中,我们想要获取名称为 Raleigh sport 的产品的订单项目 ID,并且我们只想获取其 sizeS

的产品

简单的函数调用:

print_r(get_all_orders_that_have_a_product_variation( "Raleigh Sport", "Size", "S" ));

谢谢。