Posted on

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. Enjoy!

WooCommerce: display products bought by user via a shortcode

PHP Snippet: Display All Products Purchased by User – WooCommerce


/**
 * @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.4.5
 */

// Note: this will only return purchased products for completed and processing orders

add_shortcode( 'my_products', 'bbloomer_user_products_bought' );

function bbloomer_user_products_bought() {
global $product, $woocommerce, $woocommerce_loop;
$columns = 3;
$current_user = wp_get_current_user();
$args = array(
    'post_type' => 'product',
    'post_status' => 'publish',
    'posts_per_page' => -1
);
$loop = new WP_Query($args);

ob_start();

woocommerce_product_loop_start();

while ( $loop->have_posts() ) : $loop->the_post();
$theid = get_the_ID();
if ( wc_customer_bought_product( $current_user->user_email, $current_user->ID, $theid ) ) {
wc_get_template_part( 'content', 'product' ); 
} 
endwhile; 

woocommerce_product_loop_end();

woocommerce_reset_loop();
wp_reset_postdata();

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):


[my_products]

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.

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

  1. 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?
    Regards

    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. πŸ™‚

  2. 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 πŸ™‚

  3. 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 πŸ™‚

  4. 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

  5. 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

  6. 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?

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

  8. 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 πŸ™‚

  9. 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

  10. 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.

    thanks!

    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

  11. 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

  12. 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!

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

  14. 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

  15. 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!

  16. 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!

      R

    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?

  17. 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 πŸ™‚

  18. 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

  19. 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?

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

    1. Brilliant, thanks Justin πŸ™‚

  21. 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 πŸ™‚

  22. 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>
      </div>
      </div>

      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/

  23. 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.

    -Joshua

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

  24. 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…

    [my_products]

    please help!
    Davide

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

  25. 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 πŸ™‚

  26. 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 πŸ™‚

  27. 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

  28. 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.

  29. 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're writing code, please wrap it between: [php] code_here [/php]

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.