Add a WooCommerce upsell section below the cart with custom PHP

WooCommerce: upsells below the cart table

On the cart page WooCommerce shows cross-sells, the products set in the “Cross-sells” field of the items in the cart. Upsells, the “better or bigger” alternatives, appear only on the product page. If the store manager fills upsells carefully and wants them on the cart page as well, one hook and the standard product template are enough.

Problem: The cart page should suggest the upsells of the products in the cart, without the products that are already in it, and look like the rest of the shop.

Solution: Hook into woocommerce_after_cart_table, collect the upsell IDs of the cart items with get_upsell_ids(), remove the products already in the cart, and output the rest with wc_get_template_part( 'content', 'product' ) inside the WooCommerce loop wrappers.

The code

add_action( 'woocommerce_after_cart_table', 'ha_cart_upsells' );

function ha_cart_upsells() {
    $cart = WC()->cart;
    if ( ! $cart || $cart->is_empty() ) {
        return;
    }

    $in_cart = [];
    $upsells = [];
    foreach ( $cart->get_cart() as $item ) {
        $in_cart[] = $item['product_id'];
        $parent    = wc_get_product( $item['product_id'] ); // variations keep upsells on the parent
        if ( $parent ) {
            $upsells = array_merge( $upsells, $parent->get_upsell_ids() );
        }
    }

    $ids = array_slice( array_values( array_diff( array_unique( $upsells ), $in_cart ) ), 0, 4 );
    if ( ! $ids ) {
        return;
    }

    $query = new WP_Query( [
        'post_type'      => 'product',
        'post_status'    => 'publish',
        'post__in'       => $ids,
        'orderby'        => 'post__in',
        'posts_per_page' => 4,
        'no_found_rows'  => true,
    ] );
    if ( ! $query->have_posts() ) {
        return;
    }

    echo '<section class="ha-cart-upsells"><h2>' . esc_html__( 'You may also like', 'textdomain' ) . '</h2>';
    wc_set_loop_prop( 'columns', 4 );
    woocommerce_product_loop_start();
    while ( $query->have_posts() ) {
        $query->the_post();
        wc_get_template_part( 'content', 'product' );
    }
    woocommerce_product_loop_end();
    echo '</section>';
    wp_reset_postdata();
}

the_post() also sets the global $product (WooCommerce hooks into it), so content-product.php and the theme’s overrides of it work as on the shop page. woocommerce_product_loop_start() prints the <ul class="products columns-4"> wrapper.

Hidden and out-of-stock products

post_status catches drafts, but not products hidden from the catalog or out of stock. Filter them out before the query:

$ids = array_filter( $ids, function ( $id ) {
    $product = wc_get_product( $id );
    return $product && $product->is_visible() && $product->is_in_stock();
} );

Cross-sells instead

If the store does not maintain upsells, keep the standard cross-sells and just move them:

remove_action( 'woocommerce_cart_collaterals', 'woocommerce_cross_sell_display' );
add_action( 'woocommerce_after_cart_table', 'woocommerce_cross_sell_display' );

NOTE: The upsells are read on every cart page view and after each AJAX update of the cart. With four products that is cheap; with many items in the cart, limit the number of cart items you read upsells from.

Update (2023): Since WooCommerce 8.3 new stores get the Cart block instead of the [woocommerce_cart] shortcode, and the block does not run woocommerce_after_cart_table. This code works with the classic cart; with the block, add a Products block or a custom block below the cart.