Menu Close

WooCommerce: Display All Products Purchased by User

default

When a WooCommerce customer is logged in, you might want to show them the list of previously purchased products (maybe in a custom “My Account” tab).

This is helpful when customers tend to buy the same products over and over again, and therefore you can help them “order again” without having them to search the whole product catalog again.

WooCommerce: display products bought by user via a shortcode

PHP Snippet: Display All Products Purchased by WooCommerce Customer

Once you upload the snippet to your website, you can use the following shortcode wherever you need. Please note it only works when the user is logged in, so for example the My Account page would be ideal:

[my_purchased_products]

/**
 * @snippet       Display Products Purchased by User - WooCommerce
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 8
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */
 
add_shortcode( 'my_purchased_products', 'bbloomer_products_bought_by_curr_user' );
  
function bbloomer_products_bought_by_curr_user() {
  
    // GET CURRENT USER
    if ( 0 == get_current_user_id() ) return;
  
    // GET USER ORDERS (COMPLETED + PROCESSING)
    $customer_orders = wc_get_orders( array(
        'limit' => -1,
        'customer_id' => get_current_user_id(),
        'status' => array_keys( wc_get_is_paid_statuses() ),
        'return' => 'ids',
    ) );
  
    // LOOP THROUGH ORDERS AND GET PRODUCT IDS
    if ( ! $customer_orders ) return;
    $product_ids = array();
    foreach ( $customer_orders as $customer_order_id ) {
        $order = wc_get_order( $customer_order_id );
        $items = $order->get_items();
        foreach ( $items as $item ) {
            $product_id = $item->get_product_id();
            $product_ids[] = $product_id;
        }
    }
    $product_ids = array_unique( $product_ids );
    $product_ids_str = implode( ",", $product_ids );
  
    // PASS PRODUCT IDS TO PRODUCTS SHORTCODE
    return do_shortcode("[products ids='$product_ids_str']");
  
}

Advanced Plugin: WooCommerce Product Table

In case you don’t feel like coding, I also researched plugins for displaying products purchased by the user. I discovered that the well-known WooCommerce Product Table plugin by Barn2 has a possible workaround. 

This plugin lets you list any or all of your products in a searchable table layout. Most people use it to add a table view to their public-facing shop.

However, it also has a hidden feature where it lists products previously purchased by the current user. As with the above code snippet, I recommend adding this to an area for logged in users such as the WooCommerce Account page. 

For full setup instructions, Barn2 have provided a helpful tutorial about how to use WooCommerce Product Table to list previously purchased products.

View Source
Posted in WooCommerce Tips