1
votes

I'm trying to create specific Woo templates for product categories in WooCommerce.

I've tried to follow the instructions here but they didn't work for me. Woocommerce single product - template by categories

I also found this but couldn't get it to work. http://wordpress.org/support/topic/plugin-woocommerce-excelling-ecommerce-a-custom-single-product-template

In my theme I have a woocommerce folder with the specific template files that I want to override.

theme/woocommerce/single-product.php
theme/woocommerce/content-single-product.php
theme/woocommerce/content-single-product-custom-1.php

here is part of single-product.php

<?php if (is_product_category( 'custom-1')) {
    wc_get_template_part( 'content', 'single-product-custom-1' );
    }
    elseif (is_product_category( 'promise') {
    wc_get_template_part( 'content', 'single-product-custom-2' );
    }
    else{
    wc_get_template_part( 'content', 'single-product' );
    } 
?>

I have the custom-1 category setup in woo on the specific product I want to target.

When I edit content-single-product-custom-1.php nothing changes. However when I make changes to content-single-product.php they are show up.

Why am I not able to target the my product categories?

Thanks in advance!

2

2 Answers

1
votes

The problem lies in the fact that is_product_category() behaves the same as is_category() or is_tax() Wordpress functions, which means that it checks if a category archive page ( in this case a custom taxonomy archive page ) is being displayed. And you are in single post page, you can use functions like has_term() ( to check if the current post has any of the given terms ) or get_the_terms() ( to retrieve the terms of the taxonomy that are attached to the post ), in WooCommeerce the taxonomy is 'product_cat'.


<?php 
    if ( has_term( 'custom-1', 'product_cat' ) ) {
        wc_get_template_part( 'content', 'single-product-custom-1' );
    } elseif ( has_term( 'promise', 'product_cat' ) ) {
        wc_get_template_part( 'content', 'single-product-custom-2' );
    } else {
        wc_get_template_part( 'content', 'single-product' );
    } 
?>
0
votes

Add this to your functions.php file toinclude the product_cat_CATEGORYNAME term in your products body class.

function woo_custom_taxonomy_in_body_class( $classes ){
if( is_singular( 'product' ) )
{
    $custom_terms = get_the_terms(0, 'product_cat');
    if ($custom_terms) {
        foreach ($custom_terms as $custom_term) {
            $classes[] = 'product_cat_' . $custom_term->slug;
        }
    }
}
return $classes;
}

add_filter( 'body_class', 'woo_custom_taxonomy_in_body_class' );

The product_cat term can now be referenced