0

I am searching around but is it possible in Woocommerce to show a list of users that has bought the product in the frontend?

So on the product page i wanna show a list of users that also has bought that product.

Maanstraat
  • 1,241
  • 7
  • 34
  • 63
  • The following already contains a big part of the answer to your question - [WooCommerce: Get List of Users Who Purchased a Product ID](https://www.businessbloomer.com/woocommerce-get-list-of-users-who-purchased-a-product-id/). However, the search for recommendations and pure code-writing requests are [off-topic](https://stackoverflow.com/help/on-topic) on Stack Overflow, so please adjust your question so that it conforms to the [guidelines](https://meta.stackoverflow.com/questions/260648/stack-overflow-question-checklist) – 7uc1f3r Nov 30 '20 at 12:23

2 Answers2

1

Oke i have fixed it with a little help of this question.

So what i do i put this in a function:

function retrieve_orders_ids_from_a_product_id( $product_id ) {
    global $wpdb;
    
    // Define HERE the orders status to include in  <==  <==  <==  <==  <==  <==  <==
    $orders_statuses = "'wc-completed'";

    # Get All defined statuses Orders IDs for a defined product ID (or variation ID)
    return $wpdb->get_col( "
        SELECT DISTINCT woi.order_id
        FROM {$wpdb->prefix}woocommerce_order_itemmeta as woim, 
             {$wpdb->prefix}woocommerce_order_items as woi, 
             {$wpdb->prefix}posts as p
        WHERE  woi.order_item_id = woim.order_item_id
        AND woi.order_id = p.ID
        AND p.post_status IN ( $orders_statuses )
        AND woim.meta_key IN ( '_product_id', '_variation_id' )
        AND woim.meta_value LIKE '$product_id'
        ORDER BY woi.order_item_id DESC"
    );
}

and then i use this to display their "display name" and a link to the profile:

$product_id = $product->get_id(); // Put here the product ID.
$orders_ids = retrieve_orders_ids_from_a_product_id( $product_id );
$orderID_array = array_unique($orders_ids);

echo '<ul>';

foreach ( $orderID_array as $orderID ) {
    $order = wc_get_order( $orderID );
    $profile_name = get_the_author_meta( 'display_name', $order->get_user_id() );
    $profile_link = get_author_posts_url($order->get_user_id());
    
    echo '<li><a href="' . $profile_link .'">' . $profile_name . '</a></li>';
    
}

echo '</ul>';
Maanstraat
  • 1,241
  • 7
  • 34
  • 63
0

Not sure if your users would appreciate this, but I believe that that WooCommerce offers a hook to fetch orders.

$orders->get_items();

You could loop through this and get the user() information. Obviously you would need to make sure that information that isn't "personal", so no email etc. and only the first name for example.

Check this out: https://www.businessbloomer.com/woocommerce-easily-get-order-info-total-items-etc-from-order-object/

Humpff
  • 318
  • 1
  • 10