The PHP code in this downloadable product enables you to use a custom upsells template to display upsell products conditionally on single product pages in WooCommerce.
In this example we add a conditional check to display upsells only on products which are out of stock.
Any upsells added in LInked Products will only display if the single product is out of stock.
You can use any conditional check you like to control where upsells are displayed and fully customize the custom upsells function and template without affecting the default functionality and templates.
- Copy and paste the PHP code from the functions file to the end of your child themes functions file or code snippets plugin.
Upload the single-up-sells.php template file to your child theme folder at the same level as your functions file.
Note : The functions file also includes 2 lines of code to remove the default upsells loop and related products loop.
If required, swap out the
woocommerce_after_single_product_summary hook on line 56 with any other WooCommerce single product page hook to reposition your upsells to another location on the page.
Modify the conditional on line 61 to control the output of your upsell products conditionally on single product pages.