Create a single product template
I need to make a custom template for each product on my woocommerce site.
Is there a way to do something like this, like single-product-9455.php
I believe I can create a template using categories like
<?php
if ( has_term( 'custom-1', 'product_cat' ) ) {
wc_get_template_part( 'content', 'custom-1' );
}
elseif ( has_term( 'custom-2', 'product_cat' ) ) {
wc_get_template_part( 'content', 'single-product-custom-2' );
}
else {
wc_get_template_part( 'content', 'single-product' );
}
?>
But this will lead to a lot of unnecessary categories as every product needs a custom template.
Is there a way to customize the product ID?
The body css class is not enough. I need a template for each.
source to share
You can, but you don't want to. It can get very messy if / if the site grows. A sensible direction would be to use conventions .
Otherwise, if bunny hole sounds good, you might want to create a new category for each product to link the conditional ...
1) Create a woocommerce folder in your theme directory if it doesn't exist.
2) In this directory called "woocommerce" duplicate the content-single-product.php file and name the new content-single-product-TEMPLATENAME.php
Also copy the woocommerce single-product.php template file from woocommerce to your directory named woocommerce and find the following line:
3) Find the following line in single-product.php that you just copied.
woocommerce_get_template_part( 'content', 'single-product' );
Change this to:
if (is_product_category( '1CATEGORY') // Where this is the category name
{
// Category name 1
woocommerce_get_template_part( 'content', 'single-product-TEMPLATENAME' );
}
elseif (is_product_category( '2CATEGORYNAME')
{
// category name 2
woocommerce_get_template_part( 'content', 'single-product-TEMPALTENAME2' );
}
else
{
// You will allows want to default to the single product template.
woocommerce_get_template_part( 'content', 'single-product' );
} //endif
If there is a way to intercept the product ID that you don't need. This would require the product to be there in advance, which doesn't make sense from a development / business perspective.
Changes are welcome.
source to share