WooCommerce - How to create multiple templates of one product based on a category?

Hi, I'm pretty new to woocommerce. My store has four product categories that use the same template for the same product. I want to add a fifth category of products, where the product page layout is very different from the one already used.

Here the file structure is

  • theme / woocommerce / single -product /
  • Theme / WooCommerce / Monoproduct layout /
  • Theme / WooCommerce / Monoproduct-layout / title.php
  • Theme / WooCommerce / Content-monoproduct-mock.php
  • Theme / WooCommerce / single-product.php

I created a file called content-single-product-mock.php. In single-product.php using the following code

<?php if (has_term( 'mock', 'product_cat' )) { woocommerce_get_template_part( 'content', 'single-product-mock' ); } else{ wc_get_template_part( 'content', 'single-product' ); } ?> 

For the mock category, it is redirected to content-single-product-mock.php, but uses the template files in the same product folder. How to change the template path in content-single-product-mock.php so that it uses customized files in a single product folder?

+9
php wordpress woocommerce
source share
1 answer

There are probably several ways to do this, but all of them are partially inclined to the idea of ​​filtering the template before turning it on.

You can completely skip the WooCommerce simple-product.php template (without having to redefine this template) and go directly to simple-product-mock.php and create everything there. You would do this by filtering template_include .

 add_filter( 'template_include', 'so_25789472_template_include' ); function so_25789472_template_include( $template ) { if ( is_singular('product') && (has_term( 'mock', 'product_cat')) ) { $template = get_stylesheet_directory() . '/woocommerce/single-product-mock.php'; } return $template; } 

You can edit single-product-mock.php to call content-single-product-mock.php and hardcode this file. Nothing requires you to continue to use Woo hooks and functions. Their meaning is to make it easier for you to configure.

Or, to be really tricky, you can copy templates like single-product/title.php to single-product/title.php single-product-mock ... for example: single-product-mock/title.php and then anytime we are on a single product template in the mock category, we will intercept calls to the single-product/something.php template and redirect them to single-product-mock/something.php if it exists, and continue to point to single-product/something.php if it is not. We will do this through the woocommerce_locate_template filter.

 add_filter( 'woocommerce_locate_template', 'so_25789472_locate_template', 10, 3 ); function so_25789472_locate_template( $template, $template_name, $template_path ){ // on single posts with mock category and only for single-product/something.php templates if( is_product() && has_term( 'mock', 'product_cat' ) && strpos( $template_name, 'single-product/') !== false ){ // replace single-product with single-product-mock in template name $mock_template_name = str_replace("single-product/", "single-product-mock/", $template_name ); // look for templates in the single-product-mock/ folder $mock_template = locate_template( array( trailingslashit( $template_path ) . $mock_template_name, $mock_template_name ) ); // if found, replace template with that in the single-product-mock/ folder if ( $mock_template ) { $template = $mock_template; } } return $template; } 

Update the filter instead of wc_get_template .

 /** * Change wc template part for product with a specific category * * @param string $templates * @param string $slug * @param string $name * @return string */ function so_25789472_get_template_part( $template, $slug, $name ) { if ( $slug == 'content' && $name = 'single-product' && has_term( 'test', 'product_cat' ) ) { $template = locate_template( array( WC()->template_path() . 'content-single-product-test.php' ) ); } return $template; } add_filter( 'wc_get_template_part', 'so_25789472_get_template_part', 10, 3 ); 
+15
source share