微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

给定变体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

}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐