WooCommerce: Display All Products Purchased by User

When a WooCommerce customer is logged in, you might want to show the list of purchased products, for example in a custom “My Account” tab.

I decided to code this as a shortcode, so that you can use the snippet anywhere in your WooCommerce templates, as long as the user is logged in and as long as it has processing or completed orders. Enjoy!

WooCommerce: display products bought by user via a shortcode

PHP Snippet: Display All Products Purchased by User – WooCommerce

Update: this snippet has now been completely revised for better performance. The previous version was running a query on ALL products, and websites with thousands of products were returning memory limit errors.

This optimized version simply loops through the user orders, and then gets the products inside those orders. As you can see, instead of looking through 1000s of products, we only select the right ones.

 * @snippet       Display All Products Purchased by User via Shortcode - WooCommerce
 * @how-to        Watch tutorial @ https://businessbloomer.com/?p=19055
 * @sourcecode    https://businessbloomer.com/?p=22004
 * @author        Rodolfo Melogli
 * @compatible    Woo 3.5.4
 * @donate $9     https://businessbloomer.com/bloomer-armada/

add_shortcode( 'my_products', 'bbloomer_user_products_bought' );

function bbloomer_user_products_bought() {

    global $product, $woocommerce, $woocommerce_loop;
    $columns = 3;

    // GET USER
    $current_user = wp_get_current_user();

    $customer_orders = get_posts( array(
        'numberposts' => -1,
        'meta_key'    => '_customer_user',
        'meta_value'  => $current_user->ID,
        'post_type'   => wc_get_order_types(),
        'post_status' => array_keys( wc_get_order_statuses() ),
    ) );

    if ( ! $customer_orders ) return;
    $product_ids = array();
    foreach ( $customer_orders as $customer_order ) {
        $order = new WC_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 );

    $args = array(
	   'post_type' => 'product',
	   'post__in' => $product_ids,
    $loop = new WP_Query( $args );

    while ( $loop->have_posts() ) : $loop->the_post();
	wc_get_template_part( 'content', 'product' ); 

    return '<div class="woocommerce columns-' . $columns . '">' . ob_get_clean() . '</div>';


And then use the shortcode where you need (it will only work when the user is logged in, so for example the My Account page would be ideal):


Where to add this code?

You can place PHP snippets at the bottom of your child theme functions.php file (before "?>" if you have it). CSS, on the other hand, goes in your child theme style.css file. Make sure you know what you are doing when editing such delicate files - if you need more guidance, please take a look at my free WooCommerce Customization video tutorial.

Does this snippet still work?

Please let me know in the comments if everything worked as expected. I would be happy to revise the snippet if you report otherwise (please provide screenshots). I have tested this code with Storefront theme, the WooCommerce version listed above and a WordPress-friendly hosting on PHP 7.

If you think this code saved you time & money, please join other Business Bloomer supporters and avail of 365 days of WooCommerce benefits. Thank you in advance :)

Need Help with WooCommerce?

Check out these free videos, tutorials and tips!

  • how-to-edit-woocommerce-with-php-snippets
  • woocommerce-hooks-add_action-list-visual
  • woocommerce-customize-single-product-page-PHP
  • woocommerce-customize-shop-page-PHP
  • woocommerce-advanced-customization
  • how-to-edit-woocommerce-cart
  • woocommerce-customize-checkout-page-PHP
  • woocommerce-email-customization
  • woocommerce-conditional-logic

Rodolfo Melogli

Author, WooCommerce expert, WordCamp speaker and Internet marketer, Rodolfo Melogli has worked as a WooCommerce freelancer since 2011. He helps entrepreneurs and developers overcome their WooCommerce nightmares :) Rodolfo is the organiser of WordCamp Dublin, the Dublin WooCommerce Meetup, the Dublin Ecommerce Meetup and the Dublin WordPress Meetup. He enjoys interacting with people, travelling and chasing tennis & soccer balls. Of course, he loves pizza too.

96 thoughts on “WooCommerce: Display All Products Purchased by User

  1. I copied the code to themes function (function.php) via wordpress editor but there’s a syntax error on lines of:

    global $product, $woocommerce, $woocommerce_loop;




    saying “syntax error, unexpected ‘<', expecting variable (T_VARIABLE) or '{' or '$'"
    I'm no coder and havent got any idea how it went wrong and how to fix it

    1. Hello Nikka, thanks so much for your comment! Unfortunately this looks like custom troubleshooting work and I cannot help here via the blog comments. Thanks a lot for your understanding! ~

  2. Great plugin, thank you!
    I was wondering if there was a way to display only the products that are NOT purchased by the user?
    (It would be a great way to increase sales, if users could see what products they bought, and what is still there to buy.)

    1. Hello there, thanks so much for your comment! Yes, this is possible – unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. Thanks a lot for your understanding! ~R

  3. Doesn’t this show other products when


    is empty? In my case it does. For the other cases, it works fine but when the user hasn’t bought any products it just displays some random products. problem comes because of


    . refer the following stackoverflow post https://stackoverflow.com/questions/23247671/wordpress-query-showing-post-in-empty-post-in-array

    So I suggest you do the following

        //loop through the products
    }else {
        return "<div>When you have bought products it will appear here.</div>"
    1. Fantastic, thanks for that!

  4. works like charm, thank you.
    I had a problem with a theme which made conflict and I see [my_products] in the account page. but with your snippet I could see really my products.
    Many thanks

    1. Brilliant!

  5. I want to display just few products, lets say 6 products. And have a link to view all the purchased product.

    1. Hello Anamol, thanks so much for your comment! Yes, this is possible – unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. Thanks a lot for your understanding! ~R

  6. Hello – I am not able to get the shortcode to work _ Theme X Pro

    1. It should work now πŸ™‚

  7. Hi, thanks for this great code! πŸ™‚
    Just want to share how I solved if product are not showed, you need to remove:

    'posts_per_page' => -1

    Hope this help!
    Best regards,

    1. Fantastic, thanks a million πŸ™‚

  8. Hi Rodolfo!
    Your snippet is great!!
    I need to adapt it so it shows the sold product by author, is this possible?


    1. Thanks Carla πŸ™‚ Yes, this is possible – but unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding! ~R

  9. Hello!
    How to place that short code in one of the pages of “my account” of WooCommerce. This function could insert directly into “My Account” by default or how would it be using the shortcode?

    1. Hello! Check this tutorial: https://businessbloomer.com/woocommerce-how-to-customize-the-my-account-page/ – hope this helps πŸ™‚

    2. Thank you, but It’s not working for me in the latest version of Wocommerce 3.4.7

    3. Works! I was forgetting to go edit my menu for woocommerce in appearances.

    4. πŸ™‚

  10. Hello Thank you so much for this amazing code
    I am getting an error:

    Line 34: ‘post_type’ => ‘product’,
    35 ‘post_status’ => ‘publish’,
    36 ‘posts_per_page’ => -1

    our PHP code changes were rolled back due to an error on line 34 of file wp-content/themes/astra-child/functions.php. Please fix and try saving again.

    syntax error, unexpected ‘=’, expecting ‘)’

    1. Kevin, thanks for your comment πŸ™‚ I’ve revised the snippet, try again πŸ™‚

    2. Thank you so much –
      I have added the code in my childs functions.php and added the shortcode to the desired page.
      When I do this your shortcode is the only item that renderes on the site – if i remove the shoertcode all other content comes back – I am more than happy to pay for your service as this is a need I need.
      I reached out to Bryce at metorik.com because i love how this works on his platform and he informed me that you are a metorik.com user as well πŸ™‚ . Wow love that platform – is there any way we can connect?
      Thank you kindly

    3. Thanks for your comment Kevin – I just got back to you via email as per your request. Cheers πŸ™‚

  11. Rodolfo, I am using the Storefront theme w/only the plugin for Woocommerce enabled. I added your snippet to the functions.php, added the shortcode to the My Account page, then logged in however I’m not seeing any orders. The account I logged in has plenty of orders, albeit refunded and I’m running PHP 7 and up to date w/Wordpress and Woocommerce. Any suggestions on getting this to work?

    1. Hey Jonathan – thanks so much for your comment! This snippet will only consider “completed” and “processing” orders πŸ™‚

  12. Hello Rodolfo,
    I am writing to ask for your service and support.
    I have a website i am working on. But the stage i am now i need expert support on woocommerce and reporting/statistics.
    Where can i send you my detail request by email?
    I want to send you my request so you can do the review and give me some quote.
    I will be grateful to know where i can direct this.

    1. Hey Lenu, thanks so much for your comment! If you’d like to get a quote, feel free to contact me here. Thanks a lot! ~R

  13. Hi,
    Thanks for the article , it is very helpful and interesting. i am have some difficulties writing the sql command to display the all the product purchase by a user. can you help me with the SQL command to display all the products purchase by a user?

    1. Hi Giddeon, thanks so much for your comment! Yes, this is possible – but unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. Thanks a lot for your understanding! ~R

  14. Hi. First, thank for this code.
    But, I have a problem with this. DonΒ΄t work for me.. Show me fatal error on line 6079 :

    add_shortcode( ‘my_products’, ‘bbloomer_user_products_bought’ );

    Can u help me please?

    1. Hey there, thanks for your comment! What does the fatal error say? Syntax error?

  15. Hello Rodolfo! Thank you for the great resource you are for the WooCommerce community.

    I’m running WP 4.9.7, WC 3.4.3, Genesis 2.6.1 and child theme Executive Pro 3.2.3, PHP 7.0.30-0ubuntu0.16.04.1– and cannot get products to display. The logged in customer has a completed order.

    I’ve added the shortcode [my_products] to the My Account page and the .php to the child theme’s functions.php file.

    When I remove the php, the shortcode shows up as plain text, with the php added, the shortcode disappears.

    Any assistance would be greatly appreciated.

    1. Hey Brian, thanks so much for your comment! This works for me, so please check if the product is “visible”, “published” and if the order is completed/processing. Let me know!

    2. Hey Rodolfo,

      I have experienced the same problem as Brain, I am on WordPress 4.9.7 using the DIVI theme, WC Version 3.4.3, I have added php snippet on the DIVI child theme function.

      I checked using the inspector that the inspector on the Front end a HTML is generated with the class “woocommerce columns-3” but the products inside it are empty.

      If you could please check and let use know would really appreciate

    3. Hey Nandip – try switching theme for a moment and disabling all plugins but WooCommerce. Does it work in this case? If yes, you have a plugin/theme conflict

  16. Hello,

    I update my WooCommerce to 3.4.2, Suddenly the product I purchased it doesn’t show now on my Account page.
    Basically I update also the code which is the latest compatible to 3.4.2 but still it doesn’t show.

    Any thoughts?

    Thank you.

    1. Works for me on 3.4.2 Alvin πŸ™‚

  17. Hi,

    Firstly thank you for creating this amazing resource, I think I implement your codes and learn from this site daily!

    Unfortunately this code doesn’t seem to be working for me? I have Woo3.3.5, WP4.9.5, Flatsome Theme, PHP 7, and can’t see any obvious red flags in the system report. Adding this functionality isn’t a priority but if someone finds a potential issue/fix let me know πŸ™‚ I’ll continue trying to work it out myself when I have time!

    Thanks again, Liz

    1. Hey Liz πŸ™‚ I just retested it on Woo 3.3.5 and it works. Try switching theme for a moment and see if the snippet applied to another theme works. It could be that Flatsome requires some customization of such snippet, and this could be a way to understand this. Let me know

  18. Brilliant, now do you know a way to connect this to the order form, so that a returning customer about to make a purchase would be warned they already bought this? Ideally if I could also insert a link to their Orders history it would be great.


    1. Hey Andre, thanks so much for your comment! Yes, this is possible – but unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. Thanks a lot for your understanding! ~R

  19. Thank you SO much! This is exactly what I was looking for! I was wondering though, Is there any way to modify the code to remove the price and ‘add to cart’ button since they have already purchased the products?

    Thanks so much in advance!

    1. Hello Philip, thanks so much for your comment! Yes, this is possible – but unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. Thanks a lot for your understanding! ~R

  20. Hiya,
    Looks like this “smart” dude copy-pasted everything without changing even a bit, lol: smarterdevs.com/woocommerce-add-wp-menu-wc-pages-via-php

    1. Eheh thanks Nazar πŸ™‚ I love when people copy me, it’s a good sign!

  21. Worked great! Thank you very very much! You are amaizning πŸ˜€

  22. Hi Rodolfo,
    First I want to thank you for all your information, tips and snippets, they’re very useful! I learned a lot through your site!

    I used this snippet, but I only got 2 products in stead of all purchased products. I took me a long time searching and trying to change your snippet, but then I found this snippet too: https://www.codesd.com/item/woocommerce-list-all-the-products-previously-ordered.html

    That one works for me!

    1. Mieke, thanks for that! You can simply remove the “posts_per_page” thing in my snippet and see if it works πŸ™‚

    2. After testing I had the same issue, used your suggestion and fixed it too. πŸ™‚ Thank you Mieke

  23. O correto Γ© ser feito assim:

    $customer_orders = get_posts(array(
    			'numberposts' =&gt; -1,
    			'meta_key'    =&gt; '_customer_user',
    			'meta_value'  =&gt; get_current_user_id(),
    			'post_type'   =&gt; wc_get_order_types(),
    			'post_status' =&gt; array('wc-completed', 'wc-processing'),

    Do jeito que vocΓͺ fez, consome muito o servidor , Γ© uma lΓ³gica de amador pegar todos os produtos.

    1. Hey Marcelo, thanks so much for your comment! Given that according to you my version uses a lot of memory, can you please post the whole revised snippet here? Thank you in advance!

    2. Marcelo, estou procurando fazer a mesma coisa em uma das minhas lojas, mas como o Rodolfo pediu, seria muito legal se vc pudesse compartilhar o snippet completo com as suas correçáes. O que acha? Valeu, abraço!

  24. Hi, Its not working on my side. Did exactly the same thing but not working πŸ™

    1. hey Jerina. I just retested this again on Woo 3.2.1 and Storefront theme and it still works. There must be something else on your end but I’m afraid I can’t help with custom troubleshooting here via the comments.

      To troubleshoot, go to WP Dashboard > WooCommerce > System Status: what errors do you see in red font?

      Also, take a look at this tutorial to see how to troubleshoot: https://businessbloomer.com/woocommerce-troubleshooting-mistakes-to-avoid/

      Finally, can you try switching temporarily to “Twentyseventeen” or “Storefront” theme and let me know if it works?

      Hope this helps!


    2. Thank you very much for your answer. I have tested on Twentyseventeen still not see anything. And I don’t see any error in System status. All are fine.

      I don’t why this is not working.

    3. Uhm, let’s see πŸ™‚ Are you on PHP 7?

  25. Hello and thanks
    in my case the code does not work does not return any results even if the user has completed the purchases, I tried with the store store and WooCommerce 3.1.2 shop can you help?

    1. Hey Marvy πŸ™‚ I just retested this again on Woo 3.1.2 and it still works. There must be something else but I’m afraid I can’t help with custom troubleshooting here via the comments. Thanks for your understanding πŸ™‚

  26. Is there a way to limit this to a specific category?

    1. Jonathan, thanks so much for your comment! Yes, this is possible – but unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. Thanks a lot for your understanding! ~R

  27. Hi, i follow your instruction and it’s not working ;(
    i tried using [my_products] on page and echo do_shortcode(‘[my_products]’);

    Please help.. thanks

    1. Jmarck, thanks for your comment! Were you logged in with a user who purchased some products?

    2. I agree, i am also not seeing anything and i am logged in as someone who purchased.. is this only for php 7, maybe because i am still using 5 it doesn’t work?

    3. What WooCommerce version are you guys using?

  28. Just wanted to say thanks. This worked great! I appreciate you sharing.

    1. Brilliant, thanks Justin πŸ™‚

  29. This isn’t working for me neither, I have the WOO 3.1.2, have you checked the snippet on this version?

    1. Yep Manny, just tested again, it works on Woo 3.1.2. Products must be visible and in the catalog in order to show, maybe you need to play with the ‘meta_query’ part πŸ™‚

  30. Good afternoon and thank you for this advise – should this still work for the latest Woo? Only say this because I have inserted the code exactly how is instructed and on my ‘completed orders’ account page it displays nothing.

    First I made sure orders had been completed.

    Then I inspected the code generated and I can see:

    but thats it

    any advise? Thank you

    1. Sorry – the code is:

      <ul class="products"> </ul>
    2. Are you logged in with the user who purchased those products?

    3. Sorry Rodolfo I did not see you had commented – yes this use has 2 completed orders but nothing is displayed on the [my-products] page. I can see in the code it displays this:

       <div class="woocommerce-MyAccount-content">
      <div class="woocommerce columns-3">
      <ul class="products"> </ul>

      So it is generating something…. Any advice? Thank you

    4. Jason, thanks for your comment! Are products “visible” in the shop? Otherwise I don’t know – it works on my test site. Try taking a look at this tutorial to see how to troubleshoot: https://businessbloomer.com/woocommerce-troubleshooting-mistakes-to-avoid/

  31. Hi Rodolfo!

    Quick question, would you know how to output another text/link if the user hadn’t purchased anything?
    Or would this be more of a customization question.


    1. Hey Joshua, thanks for your comment! Yes, this goes beyond the snippet specs, so I’m afraid I can’t help here πŸ™‚

  32. Hi Rodolfo,

    I inserted the php function in functions.php and the shortcode in the My account page but on the frontend i only view the shortcode…


    please help!

    1. Hey Davide, thanks for your comment! Weird πŸ™ Have you changed anything in the snippet?

  33. I just love Business Bloomer, i just love your teaching method i wish you were my teacher in university. Your articles are just amazing i have learned too much from you, your’s articles and Business bloomer.

    1. Thank you Danish… I love your comment πŸ™‚

  34. Hi,
    Do you have any update for Woocommerce 3.XXX??
    We just tested this code, but I think need some changes?
    Any update?

    1. Hey Marco, I just tested it on 3.0.4 and it works πŸ™‚

  35. Hello it works but i only want to show this in my account dashboard. How is that possible because placing the shortcode in my-account page displays it in all pages linked to my account i.e. in order page, edit address page, and even when user log outs the heading is shown in the login page yet. So i only want the products bought to be displayed in Dashboard of my account page.

    1. Hey Sarvesh, thanks for your comment! Try using this guide https://businessbloomer.com/woocommerce-visual-hook-guide-account-pages/ and identifying the correct hook πŸ™‚

    2. i am abit new to php so it woild be a grt help if u could guide how to use this in woocommerce_account_dashboard only

    3. Sarvesh, thanks so much for your comment! Yes, this is possible – but unfortunately this is custom work and I cannot provide a complementary solution here on the blog. Hopefully I will have time to create a snippet soon πŸ™‚ Thanks a lot for your understanding! ~R

  36. Hi Rodolfo,

    This also doesn’t work on my client’s site.

    I added the snippet to functions.php and [my_products] shortcode to the My Lessons page. Nothing appears in .

    I tested it with a test purchase using PayPal Sandbox, and with a free product (which also goes through checkout). Neither show up.

    I was using a different script to achieve the same thing, but this seemed to break after I updated Woocommerce to 3.0. Perhaps the update also affects your script?

    I am trying to use this script to display online music lessons purchased on this site on a page called My Lessons. I am using a plugin WooCommerce PayPerView to replace the product page content with the lesson content (embedded video and audio clips and links to PDF files). So when a customer (student) clicks on the product (lesson) title or thumbnail, they are taken to the product page, which now displays lessons content.

    Getting your script to work would make this possible. Thanks for your work!

    1. Hey Denis, thanks for your comment! Yes this is possible – when I update to Woo 3.0+ I will put this on my to-do list. Thank you! πŸ™‚

    2. It seems to work fine with Woo 3.0.6, and my theme is over two years old. When Woo 3 was released I had to replace a bunch of child files in my theme with the current ones… you might want to check WooCommerce System Status for any incompatible overrides if you haven’t tried that already.

  37. I added the code to the bottom of functions.php

    Added the shortcode [my_products] to My Account Page which already had the shortcode [woocommerce_my_account].

    When that didn’t work I removed the [woocommerce_my_account] shortcode and just used [my-products] shortcode on My Account Page and that didn’t work either.

    I have both shortcodes on My Account page and All Products Purchased by User do not show up.

    1. Hey Stacey, this works on my own website. Are you sure that particular User has purchased something?

    2. Stacey, any luck? I am having the same problem using Storefront theme and only the plugin Woocommerce enabled.

Questions? Feedback? Support? Leave your Comment Now!
If you are writing code, please wrap it between: [php]code_here[/php]. Failure to complying with this (as well as going off topic) will result in comment deletion. You should expect a reply in about a week - this is a popular blog but I need to get paid work done first. Please consider joining #BloomerArmada to ask me 1-to-1 WooCommerce questions. Thank you :)

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.