Here’s some code that allows WooCommerce users to conditionally display a list of upsells on the cart page of their WooCommerce store. Note that this code was written to make the upsells contingent on the purchase of “Required” products. Basically, the user can purchase Product X only if Products A, B and C are being purchased as well. To use it, change the list of product IDs on lines 21 and 22 to the items you want to be “Required” (line 21) and the items you want to be “Upsells” (line 22).
That’s all you have to do because the code will handle the rest. When a customer’s cart contains all of the products you marked as required, a list of upsell items will be displayed above the cart. If the customer adds an upsell to the cart and later removes a required product, the upsell will be removed and a notice will be printed for the customer to know.