Hello I am fairly new to woocommerce, My shop has four categories of products which use same single-product template. I want to add a fifth category of products where the product page layout is very different from already the one used.
Here's the file structure -
- theme/woocommerce/single-product/
- theme/woocommerce/single-product-mock/
- theme/woocommerce/single-product-mock/title.php
- theme/woocommerce/content-single-product-mock.php
- theme/woocommerce/single-product.php
I created a file called content-single-product-mock.php. In single-product.php using the following code
<?php if (has_term( 'mock', 'product_cat' )) {
woocommerce_get_template_part( 'content', 'single-product-mock' );
} else{
wc_get_template_part( 'content', 'single-product' );
} ?>
For the category mock it redirects to content-single-product-mock.php, but it uses the template files in single-product folder. How do change the template path in content-single-product-mock.php so that it uses the customized files in single-product-mock folder?
There is probably more than one way to do this, but they all kind of turn around the idea of filtering the template before it is included.
You could totally skip WooCommerce's
simple-product.php
template (without needing to override that template) and go directly tosimple-product-mock.php
and create everything there. You'd do that by filteringtemplate_include
.You could edit
single-product-mock.php
to callcontent-single-product-mock.php
and hard-code that file. Nothing requires you to keep using Woo's hooks and functions. The point of them is just to make it easy for you to customize.Or to be really tricky, you could duplicate templates such as
single-product/title.php
into asingle-product-mock
folder... ex:single-product-mock/title.php
and then any time we're on a single product template in the mock category, we'll intercept calls to thesingle-product/something.php
template and redirect them tosingle-product-mock/something.php
if it exists and keep pointing to thesingle-product/something.php
if it does not. We'll do this via thewoocommerce_locate_template
filter.