0
votes

I'm trying to change the template of a specific product category page, but I can't for the life of me get it to work. I've tried taxonomy-product_cat-the-category but to no avail. I'm using wpml with the categories in 3 different languages if that makes a difference. I've tried with the slugs of each of the 3 langauges but still doesn't work. Don't understand what could be causing this.

2

2 Answers

4
votes

First copy taxonomy-product_cat.php & archive-product.php from WooCommerce Plugin directory - wordpress\wp-content\plugins\woocommerce\templates to your theme - wordpress\wp-content\themes\your-theme\woocommerce.

Then make copy of archive-product.php files in your theme's woocommerce directory like archive-product-2.php, archive-product-3.php etc. Later you are going to modify these files as per your category.

Then open taxonomy-product_cat.php file. The code will look like below one.

if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly
}

wc_get_template( 'archive-product.php' );

We need to modify the template call wc_get_template() in this code with our conditions.

First get the current category slug & then we can compare the slug. As per the slug, we will call different archive product files.

if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly
}

// Get current category slug
global $wp_query;
$cat_slug = $wp_query->query_vars['product_cat'];

// Call template conditionally
if($cat_slug == 'accessories') {
    wc_get_template( 'archive-product-2.php' );
} else  {
    wc_get_template( 'archive-product.php' );   
}

Updates

In my experience, the best approach is avoid WooCommerce template overriding by copying in to theme files. The WooCommerce have regular updates on the templates and it is better to update templates on each time to avoid future issues. So if possible, maximum use filter hooks other than template overriding.

-2
votes

You can can't change the template of WP for a specific category but

  • For only overriding design: You can do css by using body.term-yourcategoryname at starting of each class, ID or tag name like this:

    body.term-car h1 { color: red; font-size: 22px; }

  • For enhancing Layout: or add some new feature you can use wooommerce default functions like: $term->name; in archive-product.php in your child theme.

example:

if($term->name == "car") {
   // your code will goes here
}