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        businessbloomer.com/woocommerce-customization
 * @author        Rodolfo Melogli, Business Bloomer
 * @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        businessbloomer.com/woocommerce-customization
 * @author        Rodolfo Melogli, Business Bloomer
 * @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

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 *