获取与客户在 Woocommerce 中购买的产品相关的订单 ID [英] Get the orders IDs related to a product bought by a customer in Woocommerce

查看:54
本文介绍了获取与客户在 Woocommerce 中购买的产品相关的订单 ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将产品 ID 作为数组,如果客户购买了该产品,我想获取订单 ID 列表.

I have the products ids as an array and I would like to get the list of order ids if the customer has purchased that product.

我有客户购买的产品 ID.不知何故,如果客户购买新产品,我必须获取链接的订单 ID 并取消该订单.

I have the customer purchased product ids with me. Somehow, I have to get the linked order id and cancel that order if customer purchases new product.

要检查客户是否购买了产品,我正在使用此答案线程中的 has_bought_items() 函数:检查客户是否在 WooCommerce 中购买了特定产品

To check if a customer has purchased a product I am using the function has_bought_items() from this answer thread: Check if a customer has purchased a specific products in WooCommerce

是否可以对其进行调整以获得所需的输出?

May be it can be tweaked to get the desired output?

推荐答案

以下自定义函数使用一个非常轻量的唯一 SQL 查询,将从一组产品 ID(或唯一产品 ID)中获取所有订单 ID,用于给定的客户.

The following custom function made with a very light unique SQL query, will get all the Orders IDs from an array of products IDs (or a unique product ID) for a given customer.

基于以下代码:检查客户是否在 WooCommerce 中购买了特定产品

function get_order_ids_from_bought_items( $product_ids = 0, $customer_id = 0 ) {
    global $wpdb;

    $customer_id = $customer_id == 0 || $customer_id == '' ? get_current_user_id() : $customer_id;
    $statuses    = array_map( 'esc_sql', wc_get_is_paid_statuses() );

    if ( is_array( $product_ids ) )
        $product_ids = implode(',', $product_ids);

    if ( $product_ids !=  ( 0 || '' ) )
        $meta_query_line = "AND woim.meta_value IN ($product_ids)";
    else
        $meta_query_line = "AND woim.meta_value != 0";

    // Get Orders IDs
    $results = $wpdb->get_col( "
        SELECT DISTINCT p.ID FROM {$wpdb->prefix}posts AS p
        INNER JOIN {$wpdb->prefix}postmeta AS pm ON p.ID = pm.post_id
        INNER JOIN {$wpdb->prefix}woocommerce_order_items AS woi ON p.ID = woi.order_id
        INNER JOIN {$wpdb->prefix}woocommerce_order_itemmeta AS woim ON woi.order_item_id = woim.order_item_id
        WHERE p.post_status IN ( 'wc-" . implode( "','wc-", $statuses ) . "' )
        AND pm.meta_key = '_customer_user'
        AND pm.meta_value = $customer_id
        AND woim.meta_key IN ( '_product_id', '_variation_id' )
        $meta_query_line
    " );

    // Return an array of Order IDs or an empty array
    return sizeof($results) > 0 ? $results : array();
}

代码位于活动子主题(或活动主题)的 function.php 文件中.经测试有效.

Code goes in function.php file of your active child theme (or active theme). Tested and works.

1) 对于当前登录的客户(以及数组中的 2 个产品 ID):

1) For the current logged in customer (and 2 product Ids in an array):

$product_ids = array(37,53);
$order_ids = get_order_ids_from_bought_items( $product_ids );

2) 对于定义的用户 ID 和一个产品 ID:

2) For a defined User ID and one product ID:

$product_id = 53;
$user_id    = 72;
$order_ids  = get_order_ids_from_bought_items( $product_id, $user_id );

这篇关于获取与客户在 Woocommerce 中购买的产品相关的订单 ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆