9

For example i have category coffee machines, and one template for coffee machine single product template, but for category coffee i want to have different single-product.php template, how to implement this? i haven't access to this post http://www.woothemes.com/support-forum/?viewtopic=83667 but it have similar question, with pages and categories in WordPress it simple, but how to do it in woocommerce?

4 Answers 4

8

You could change your single-product.php to just be a redirect to the correct template depending on what product category the current product it.

To do so you'd copy single-product.php to your theme's woocommerce folder. Rename it to single-product-default.php or anything. Create another copy and call it single-product-coffee.php. You can make whatever changes you'd like to make to this one.

Then in your single-product.php you could add a simple conditional to redirect to the appropriate single-product-something.php

if( has_term( 'coffee-maker', 'product_cat' ) ) {
    $file = 'single-product-coffee.php';
} else {
    $file = 'single-product-default.php';
}

global $woocommerce;

load_template( $woocommerce->template_url . $file );
6

Make the new template and name it: taxonomy-product_cat-Your_category_product_slug.php as simple as this

2
  • This is the answer, even though it's not accepted. Commented Apr 6, 2018 at 17:09
  • 1
    Nope looks like this if for products listing (archive-product), not the single product page (single-product). Just take a look at the code to confirm it.
    – Buzut
    Commented Jun 12, 2020 at 8:59
3

I made a redirection in single-product.php using the product ID.

Then created different product templates in the overriden woocommerce folder (../your-theme/woocommerce/) such as single-product-product1.php and the default one (single-product-default.php, which simply was a copy of the previous single-product.php).

if($post->ID == '103'){
wc_get_template_part( 'single-product-product1' ); 
} else{
wc_get_template_part( 'single-product-default' );
}
1
  • Had to log in and give a vote to this! Thanks, saved me!
    – Tiago
    Commented Mar 31, 2020 at 15:59
1

I have done this (Woocommerce 2.1) by creating a template for the product in my theme's woocommerce templates named 'content-single-product-{$product_cat}.php' and adding a 'wc_get_template_part' filter that searches for templates named after the product category. Now you can override the content-single-product template by product category:

function my_custom_product_template($template, $slug, $name) {
    if ($name === 'single-product' && $slug === 'content') {
        global $product_cat;
        $temp = locate_template(array("{$slug}-{$name}-{$product_cat}.php", WC()->template_path() . "{$slug}-{$name}-{$product_cat}.php"));
        if($temp) {
           $template = $temp;
        }
    }
    return $template;
}

add_filter('wc_get_template_part', 'my_custom_product_template', 10, 3);

Not the answer you're looking for? Browse other questions tagged or ask your own question.