Reserve Your Free Seat for Our Next WooCommerce Class! Search
Business Bloomer
  • Business Bloomer Club
  • WooCommerce Plugins
  • WooCommerce Tips
  • Log In
  • 0
  • Business Bloomer Club
  • WooCommerce Plugins
  • WooCommerce Tips
  • Log In
  • Search
  • Contact
  • Cart
WooCommerce Code Snippets Cross-sells My Account

WooCommerce: Display Cross-Sells For All Purchased Products @ My Account

Last Revised: May 2024

STAY UPDATED

Are you looking to enhance the shopping experience for your customers on your WooCommerce store? One effective strategy is to display cross-sells for all purchased products on the “My Account” page.

Cross-selling not only helps increase your return business, but also introduces customers to products they might find useful or interesting based on their previous purchases.

In this tutorial, we’ll guide you through the steps to implement cross-sells in the “My Account” section, ensuring your customers always see relevant product recommendations based on their purchases.

Whether you’re a seasoned WooCommerce user or just starting out, this tutorial will provide you with the necessary tools and tips to boost your sales and customer satisfaction.

Here’s one of the possible positions you can show the logged in user a grid of cross-sells. They are automatically calculated from all their previous orders.

PHP Snippet: Show Grid of Cross-Sells @ WooCommerce My Account

Usage: add the [cross_sells_all_purchases] shortcode to the My Account page, and see the magic happen.

/**
 * @snippet       Get All-Time Purchased Products Cross-Sells
 * @how-to        businessbloomer.com/woocommerce-customization
 * @author        Rodolfo Melogli, Business Bloomer
 * @compatible    WooCommerce 8
 * @community     https://businessbloomer.com/club/
 */

add_shortcode( 'cross_sells_all_purchases', 'bbloomer_cross_sells_products_bought_by_curr_user' );

function bbloomer_cross_sells_products_bought_by_curr_user() {
   
   // GET CURR USER
   $current_user = wp_get_current_user();
   if ( 0 == $current_user->ID ) return;

   // GET USER ORDERS (COMPLETED + PROCESSING)
   $customer_orders = wc_get_orders( [
      'customer_id' => $current_user->ID,
      'return' => 'ids',
      'status' => array( 'wc-processing', 'wc-completed' ),
      'limit' => -1,
   ] );

   // LOOP THROUGH ORDERS AND GET CROSS-SELLS 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 = $item->get_product();
		    if ( ! $product ) continue;
          $product_ids = array_merge( $product->get_cross_sell_ids(), $product_ids );
       }
    }
    $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']" );

}

The add_shortcode function registers a new shortcode [cross_sells_all_purchases] which calls the function bbloomer_cross_sells_products_bought_by_curr_user.

The function first checks if a user is logged in. If not, it exits.

It then fetches all orders for the current user that are either ‘processing’ or ‘completed’.

If there are paid orders, it loops through each order and each item within the orders to collect cross-sell product IDs associated with those items.

Duplicate product IDs are removed.

Finally, it passes the collected product IDs to the WooCommerce [products] shortcode to display the products on the frontend.

Where to add custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

  • WooCommerce: Separate Login, Registration, My Account Pages
    There are times when you need to send logged out customers to a Login page and unregistered customers to a standalone Register page. As you…
  • WooCommerce: Add New Tab @ My Account Page
    One of the features of Business Bloomer Club is the provision of Premium WooCommerce Q&A Support to supporters who enroll. So, how to add an…
  • WooCommerce: How To Make A Website GDPR Compliant? (12 Steps)
    Ok, we all know that the EU General Data Protection Regulation (GDPR) will come into force on the 25th May 2018. So the main question…
  • WooCommerce Visual Hook Guide: My Account Pages
    Hey WooCustomizers, the Visual Hook Guide is back 🙂 In this episode, I’ve created a visual HTML hook guide for the WooCommerce Account Pages (there…
  • WooCommerce: Add First & Last Name to My Account Register Form
    Here’s yet another useful PHP snippet – and a mini-plugin alternative with super simple settings – that adds the Billing First Name and Billing Last…

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza. Follow @rmelogli

4 thoughts on “WooCommerce: Display Cross-Sells For All Purchased Products @ My Account”

  1. Vikas Sharma
    October 20, 2024

    Worked perfectly. I have modified last line of code to display products in single row and in 6 columns. If in case any one required that. You can modify the code by replacing the last line with limit=’6′ columns=’6′

    thanks

    Reply
    1. Rodolfo Melogli
      October 24, 2024

      Awesome!

      Reply
  2. D
    August 6, 2024

    Not working. Nothing happens on my account page

    Reply
    1. Rodolfo Melogli
      August 13, 2024

      Works for me though! Do the products you purchased with the logged-in user have definitely cross-sells in the edit product page?

      Reply
Questions? Feedback? Customization? Leave your comment now!
_____

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining the Business Bloomer Club to get quick WooCommerce support. Thank you!

Cancel reply

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


Search WooCommerce Tips

Popular Searches: Visual Hook Guides - Checkout Page - Cart Page - Single Product Page - Add to Cart - Emails - Shipping - Prices - Hosting

Recent Articles

  • WooCommerce: Redirect Empty Paginated Category Pages (404)
  • WooCommerce: Complete Button @ Order Admin
  • WooCommerce: Allow Guest Checkout For Existing Customers
  • WooCommerce: Simplify Free Checkout
  • WooCommerce: Inject Ad After the nth Product @ Shop Page

Latest Comments

  1. Rodolfo Melogli on WooCommerce: Separate Login, Registration, My Account Pages
  2. Rodolfo Melogli on WooCommerce: Complete Button @ Order Admin
  3. Rodolfo Melogli on WooCommerce: Failed Orders Monitor & Temporary Lockdown

Find Out More

  • Become a WooCommerce Expert
  • Business Bloomer Club
  • WooCommerce Blog
  • WooCommerce Weekly
  • Contact

Contact Info

Ciao! I'm Rodolfo Melogli, an Italian Civil Engineer who has turned into an international WooCommerce expert. You can contact me here:

Twitter: @rmelogli

Get in touch: Contact Page

Business Bloomer © 2011-2025 - VAT IT02722220817 - Terms of Use - Privacy Policy

Cart reminder?

x