skip to Main Content

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.

2

Answers


  1. Chosen as BEST ANSWER

    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>';
    

  2. 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/

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search