Roots Discourse

Multiple single-product templates

Hi guys,
I’m trying to create different product page templates for each of my categories. I’ve got an unclassed category and a “Custom” category.

Right now, i’ve tried 2 methods :

  • Adding a filter in the filters.php file like this :
    add_filter( 'template_include', 'so_43621049_template_include' );

    function so_43621049_template_include( $template ) {
      if ( is_singular('product') && (has_term( 'custom', 'product_cat')) ) {
        $template = get_stylesheet_directory() . '/views/woocommerce/single-product-custom.blade.php';
      }
      return $template;
    }
  • Modifying the code in my single-product.blade.php file like this :
    @while(have_posts())
        @php
            the_post();
            do_action('woocommerce_shop_loop');
            global $post;
            $terms = wp_get_post_terms( $post->ID, 'product_cat' );
            foreach ( $terms as $term ) $categories[] = $term->slug;
            if ( in_array( 'custom', $categories ) ) {
            wc_get_template_part('content', 'single-product-custom');
            } else {
            wc_get_template_part('content', 'single-product');
            }
        @endphp
    @endwhile

But none works… The first method doesn’t do anything, and the second method does something weird : when i open a Custom product page, i only have my header and my footer, nothing else appears on the page. The var_dump on this part returns the following :

array(1) { [0]=> object(WP_Term)#13883 (10) { [“term_id”]=> int(61) [“name”]=> string(6) “Custom” [“slug”]=> string(6) “custom” [“term_group”]=> int(0) [“term_taxonomy_id”]=> int(61) [“taxonomy”]=> string(11) “product_cat” [“description”]=> string(0) “” [“parent”]=> int(0) [“count”]=> int(1) [“filter”]=> string(3) “raw” } }

What am i missing ? What’s the best way to achieve this ?

Note : I’m using Sage 9

This should help:

Are you using

This topic was automatically closed after 42 days. New replies are no longer allowed.