Post-sale cross-sell with WooCommerce

Have you ever seen post-sale selling techniques?

Not long ago I was shopping at The Postman’s Knock (if you want to improve your calligraphy you should check them out) and noticed that their shop runs on WooCommerce.

I purchased a product, and then on the checkout page I  noticed that they have a table of products related to their courses.

In their case, they are external products, but I thought to use that section to cross-sell some products post-sale. Let’s see how to do it!

Preparing the post-sale products tempate

Open your functions.php file in wp-content/themes/your-child-theme-name/ and add this code at the end of the file:

add_action( 'woocommerce_order_details_after_order_table', 'post_sale_cross_sell' );
function post_sale_cross_sell( $order ) {
$cross_sells = array();
$in_order = array();
foreach ( $order->get_items() as $item ) {
$product_id = $order->get_item( $item )->get_product_id();
$product = wc_get_product( $product_id );
$cross_sells = array_merge( $product->get_cross_sell_ids(), $cross_sells );
$in_order[] = $product_id;
}
$cross_sells = array_diff( $cross_sells, $in_order );
$cross_sells = apply_filters( 'woocommerce_cart_crosssell_ids', wp_parse_id_list( $cross_sells ) );
?>
<section class="related products post_sale">
<h2><?php esc_html_e( 'You might be interested in…', 'domain' ); ?></h2>
<p><?php esc_html_e( 'People who purchased the same products you bought usually also buy the following ones. Care to take a look?', 'woocommerce' ); ?></p>
<?php woocommerce_product_loop_start(); ?>
<?php foreach ( $cross_sells as $product_id ) : ?>
<?php
$post_object = get_post( $product_id );
setup_postdata( $GLOBALS['post'] =& $post_object );
wc_get_template_part( 'content', 'product' ); ?>
<?php endforeach; ?>
<?php woocommerce_product_loop_end(); ?>
</section>
<?php
}
view raw functions.php hosted with ❤ by GitHub

This function will take care of getting the cross-sale products set for the products that have been purchased, and show them on the Thank you page.

Feel free to edit the text and HTML code to what you prefer most!

Settings cross-sell products

WooCommerce has a way to do it directly in the core.

Edit your product in Products > All Products and open the Linked Products tab. In the Cross-sells field choose the products that you want to cross-sell when that specific product is purchased:

A screenshot of the Linked Products tab in the Edit Product screen

That’s all! This setup will get you a result like this, on the Thank you page:


More Posts That You Might Like…


5 responses to “Post-sale cross-sell with WooCommerce”

  1. Works exactly as described. I have disabled the Related Products from the Cart page. I always thought adding to the Checkout/Thank You page would be better in not disrupting the Checkout flow.

    Thank you again for a very useful snippet!

    1. You’re welcome Amit!

  2. Well! It’s not working for me!

  3. Is there a way to make product cross-sell on the fly? In other words on the cart page have a static Id that you can make a cross-sell for any product in the cart.

  4. Hey, I tried the code and I hard coded the product Id but only the html code comes through. I do not have any cross-sell defined in woocommerce. I only have one product that I want to offer. So if $cross-sells is empty until it does the diff with $in_order[ ] will it still work.

Leave a Reply

Categories

Newsletter

Receive new articles from this blog directly in your inbox!

No spam guaranteed!

Blog at WordPress.com.

%d bloggers like this: