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.
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.
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
Awesome!
Not working. Nothing happens on my account page
Works for me though! Do the products you purchased with the logged-in user have definitely cross-sells in the edit product page?