Custom Single Product Page Templates for Each Category in WooCommerce

The code in this download folder enables you to load a custom single product page template based on the product category the single product is assigned to.

How The Code Works

The code is designed to customize the appearance of individual product pages on a WordPress website using WooCommerce.

Filter Usage :
– It utilizes a core WordPress filter to modify the template file used for rendering single product pages.

Checking if on a Single Product Page :
– It checks whether the current page being viewed is a single product page.

Retrieving Product Categories :
– It retrieves the categories assigned to the current product.

Looping Through Categories :
– It loops through each category assigned to the product.

Constructing Template File Names :
– It dynamically constructs a template file name based on the product category.

Locating Custom Templates :
– It attempts to locate custom template files within a specific folder for each product category.

Loading Custom Template :
– If a custom template is found for the product category, it replaces the original template with the custom one.

Fallback to Original Template :
– If no specific template is found, it falls back to the original template for the single product page.

Adding Body Class :
– Additionally, it adds a custom body class to the page based on the product’s category.

Dynamic Inclusion of Templates :
– Instead of hardcoding template names, it dynamically includes templates based on conditions, improving flexibility.

The code provides a flexible way to customize the display of single product pages based on their assigned categories.

Installation

There’s 3 steps :

1. Copy and paste the PHP code to the end of your child themes functions file.

2. Upload the folder named single-product-category-templates to your child theme folder.

3. Name the files according to your product category slug. Example : If the category name is Demo, the template file to load on all single products assigned the demo category will be named single-product-demo.php

You’ll need to copy the file named single-product-demo.php and rename the file for each category using the category slug.

Download Folder

Join 5000+ Followers

Get The Latest Free & Premium Tutorials Delivered The Second They’re Published.