1
votes

I am trying to create a custom layout for one of our product pages in Woocommerce. I have searched and followed so many tutorials, but none are working for me. I copied 'single-product.php' and placed it into my active child theme in the folder 'woocommerce'. I then duplicated this and renamed it 'single-product-landing.php'.

I then placed the following code into my functions.php page:

add_filter( 'template_include',     'custom_single_product_template_include', 10 );

function custom_single_product_template_include( $template ) {
if ( is_product() && ( has_term( 'custom', 'product_cat') ) ) {
$template = get_stylesheet_directory() . '/woocommerce/single-product-landing.php';
 } 
return $template;
}

Then I tried changing items in single-product-landing.php but nothing is changing, so it can't be picking it up. To confirm, I have the product assigned to the 'custom' category.

I should also add that my product is a 'variable' product, am not sure if that has any affect on anything here.

EDIT: I have found the following code in my 'single.php' file - I think this may be interfering. Problem is, if I delete this nothing shows on any of my pages (even those not affected by the new template):

 <?php
      if(get_theme_mod('product_layout') == 'custom') {
        wc_get_template_part( 'content', 'single-product-custom' );
      } else {
        wc_get_template_part( 'content', 'single-product' );
      }
  ?>

Any idea how I can modify this to still have content show but not over-rule the template change that i'm trying to make?

1
you don't need a function to override a woocommerce template, simply copy the template to your child-theme/woocommerce and keep the name the same,Jack Robson
That will change all pages. I'm trying to create a custom layout for just ONE of my product pages.IVCatalina
I see, try renaming the template to taxonomy-product_cat-Your_category_product_slug.php and put it back in the base of your child themeJack Robson

1 Answers

1
votes
  1. Page templates are loaded after the template_filter has been run, so adding that code to a page template file will have no effect. To make that run, place the same code in your child theme's functions.php.

  2. The 566 at the end of the add_filter line refers to the filter's loading priority and not a category id. Simply put, the higher the number the later the filter will be loaded.

  3. Finally, your if statement can be simplified a little using Woocommerce functions - if ( is_product() && ( has_term( 'custom', 'product_cat') ) ) { - doesn't make much difference, but it's tidier.