Custom templates in Woocommerce 3

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Custom templates in Woocommerce 3



I'm trying to make a separate template for only one product with ID 5555. To delete a photo from its page and change the blocks structure. Overriding this file affects all product pages.


wp-contentpluginswoocommercetemplatescontent-single-product.php



The logical solution is to add a condition: if the product single prpduct page have ID 5555, then another template is used for its page.
I tried this option, but it does not work.


/**
* Custom single product template.
*/

add_filter( 'single_template', 'get_custom_post_type_template' );
function get_custom_post_type_template($single_template)
global $post;

if ($post->post_type == 'product')
$single_template = dirname( __FILE__ ) . '/single-template.php';

return $single_template;


add_filter( 'template_include', 'portfolio_page_template', 99 );
function portfolio_page_template( $template )

if ( is_page( 'slug' ) )
$new_template = locate_template( array( 'single-template.php' ) );
if ( '' != $new_template )
return $new_template ;



return $template;



Or option 2: remove this hooks:


remove_action( 'woocommerce_before_single_product_summary', 'woocommerce_show_product_images', 20 );
remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20 );



from only one specific product page:




2 Answers
2



Since Woocommerce 3.3 Wordpress hook template_include doesn't seem to work always, as you could see in a lot of recent related threads in StackOverFlow and somewhere else too.


template_include



The specific filter hooks related to templates for Woocommerce:


wc_get_template


wc_get_template_part


woocommerce_locate_template


woocommerce_template_path


wc_get_template_part



If you look at single-product.php template, the content-single-product.php template is loaded with the following at this line:


single-product.php


content-single-product.php


<?php wc_get_template_part( 'content', 'single-product' ); ?>



So we will use the hook wc_get_template_part.


wc_get_template_part



Let say your custom template replacement file is named content-single-product-custom.php and located in the woocommerce folder of your active child theme (or active theme). To use this custom template for a specific product ID (5555) you will need the following:


content-single-product-custom.php


woocommerce


5555


add_filter( 'wc_get_template_part', 'custom_wc_template_part', 10, 3 );
function custom_wc_template_part( $template, $slug, $name )
// The specific product ID
$product_id = 5555;

// The custom template file name
$custom_template_name = 'content-single-product-custom.php';

// For a specific product ID and content-single-product.php template
if( get_the_ID() == $product_id && $slug == 'content' && $name == 'single-product' )
$template = trailingslashit( get_stylesheet_directory() ) . 'woocommerce/' . $custom_template_name;

return $template;



This code goes in function.php file of your active child theme (or active theme). Tested and works.


wc_get_template



If templates is loaded via wc_get_template() function, like for example single-product/meta.php and your custom replacement template is named single-product/custom-meta.php (and located in the woocommerce folder of your active child theme). To use this custom template for a specific product ID (5555) you will need the following:


wc_get_template()


single-product/meta.php


single-product/custom-meta.php


5555


add_filter( 'wc_get_template', 'custom_wc_template', 10, 5 );
function custom_wc_templates( $located, $template_name, $args, $template_path, $default_path )
// The specific product ID
$product_id = 5555;

// The custom template file name and path
$custom_template_name = 'single-product/meta-custom.php';

if( is_product() && get_the_ID() == 37 && $template_name == 'single-product/meta.php')
$located = trailingslashit( get_stylesheet_directory() ) . 'woocommerce/' . $custom_template_name;

return $located;



This code goes in function.php file of your active child theme (or active theme). Tested and works.





Thank you very much, Loic! I checked the code works fine!👍
– Александра Кузнецова
Aug 12 at 13:39



Add this to your theme's 'functions.php' where 5 is product ID and woocommerce/single-product-watermelon.php is custom product template.


add_filter( 'template_include', 'watermelon_template_include' );

function watermelon_template_include( $template )
if ( is_singular('product') && get_the_ID() == 5 ) //if category, change has_term( 'watermelon', 'product_cat') instead of get_the_ID()
$template = get_stylesheet_directory() . '/woocommerce/single-product-watermelon.php';

return $template;





There are no changes unfortunately... 5 - product id?
– Александра Кузнецова
Aug 12 at 8:02





Yes 5 is product ID.
– Outsource WordPress
Aug 12 at 8:04





First, add some characters to woocommerce/single-product-watermelon.php and confirm whether it's loading for product ID 5. Please note that WooCommerce hooks will work same as normal product template and it's better to apply your customizations directly without using WooCommerce hooks.
– Outsource WordPress
Aug 12 at 8:31



woocommerce/single-product-watermelon.php






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard