WooCommerce: Set Product Upsells Programmatically

Looking to boost your WooCommerce store’s average order value?

Upsells are a powerful tool, but manually entering them via the Edit Product page can be tedious.

This tutorial dives into the world of programmatic WooCommerce product upsells, empowering you to leverage code for a dynamic and data-driven upsell strategy.

In under 5 lines of PHP, we’ll guide you through the process of setting up upsells using code, unlocking greater control and personalization for a seamless customer experience that maximizes your sales potential. Enjoy!

Tell me you’re not annoyed by the fact you need to set the upsells FOR EACH product by hand! Well, the snippets below can help you set them programmatically.

PHP Snippet 1: Set The Same Product Upsells For All WooCommerce Products

This is super helpful if you want your products to have the same upsells storewide, and you want to avoid entering upsells manually on each Edit Product page!

/**
 * @snippet       Programmatically Set Upsells For All WooCommerce Products
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 9
 * @community     https://businessbloomer.com/club/
 */

add_filter( 'woocommerce_product_get_upsell_ids', 'bbloomer_same_upsell_ids', 9999, 2 );

function bbloomer_same_upsell_ids( $upsell_ids, $product ) {
    return array( 222239, 222166 ); // PRODUCT IDS
}

PHP Snippet 2: Set Upsells For A Single WooCommerce Product

You can of course use the $product argument that the woocommerce_product_get_upsell_ids filter gives us, and define a list of upsell IDs per product.

/**
 * @snippet       Programmatically Set Upsells For A WooCommerce Product
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 9
 * @community     https://businessbloomer.com/club/
 */

add_filter( 'woocommerce_product_get_upsell_ids', 'bbloomer_per_product_upsell_ids', 9999, 2 );

function bbloomer_per_product_upsell_ids( $upsell_ids, $product ) {
    if ( $product->get_id() == 1234 ) return array( 222239, 222166 ); // PRODUCT IDS
    if ( $product->get_id() == 4567 ) return array( 245687, 245761 ); // PRODUCT IDS
    // ETC.
}

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 Visual Hook Guide: Single Product Page
    Here’s a visual hook guide for the WooCommerce Single Product Page. This is part of my “Visual Hook Guide Series“, through which you can find WooCommerce hooks quickly and easily by seeing their actual locations (and you can copy/paste). If you like this guide and it’s helpful to you, let me know in the comments! […]
  • WooCommerce: Disable Variable Product Price Range $$$-$$$
    You may want to disable the WooCommerce variable product price range which usually looks like $100-$999 when variations have different prices (min $100 and max $999 in this case). With this snippet you will be able to hide the highest price, and add a “From: ” prefix in front of the minimum price. At the […]
  • WooCommerce: Hide Price & Add to Cart for Logged Out Users
    You may want to force users to login in order to see prices and add products to cart. That means you must hide add to cart buttons and prices on the Shop and Single Product pages when a user is logged out. All you need is pasting the following code in your functions.php (please note: […]
  • WooCommerce: Add Custom Field to Product Variations
    Adding and displaying custom fields on WooCommerce products is quite simple. For example, you can add a “RRP/MSRP” field to a product, or maybe use ACF and display its value on the single product page. Easy, yes. Unfortunately, the above only applies to “simple” products without variations (or the parent product if it’s a variable […]
  • WooCommerce: Show Number Of Products Sold @ Product Page
    WooCommerce database already stores the number of products sold for you. Therefore, you may want to show such number on the product page, close to the Add To Cart button. As we’ve seen in my book Ecommerce and Beyond, showing the number of sales for each product can increase your sales conversion rate. All you […]

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: Set Product Upsells Programmatically

  1. This is quite useful Rodolfo. Thank you.

    Please add

    // Return the fixed upsell IDs
    return $fixed_upsell_ids;

    It is also possible to check if the product belongs to a specific category and set different upsell IDs accordingly.

    1. Why? I’m already returning an array of product IDs, so I don’t need to return that afterwards.

      As per the other question, I suggest you take a look at “conditional logic”: https://businessbloomer.com/woocommerce-conditional-logic-ultimate-php-guide/

      1. Hi,
        Thanks for this improvement!
        It would indeed be nice to add the same functionality based on categories and upselling categories in case of variable products.
        example:
        category hats, category belts, category accessories
        product A: hats & accessories – upsells hats + accessories
        product B : belts & accessories – upsells belts + accessories
        Tks

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!

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