This PHP code enables the storeowner to customize exactly which related products display on each single product page in WooCommerce. The related products 1,2,3 & 4 shown in the following image below the main product image have been added manually on the Edit Product screen in the WordPress Dashboard.
By default, related products are displayed according to tags and categories.
With this code, you set the products you want displayed on each single product pages. If you don’t want any related products from displaying on any single product page, don’t add them and nothing will display. You can also use this code to display both the default related products and custom related products before or after the default display.
This enables you to control every related product on every single product page.
Installation – Solution 1
There’s 2 basic steps :
Step 1 Copy the PHP code to the end of your child themes functions file or custom functions plugin.
Step 2 Add your comma separated product ids on any Edit Product screen using a custom field named related like this :
You can find the product ids next to each product on the Products page in your WordPress Dashboard.
You’ll also find another solution in the functions-2.php file which achieves exactly the same result using exactly the same installation steps however it modifies the default related products functionality.
Multiple Related Product Loops
You can use the code from solution 1 to display multiple related post loops before and after the single product image as well as both cross-sell and category/tag-related products using the default related products functionality.