Custom templates in Woocommerce 3 - php

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-content\plugins\woocommerce\templates\content-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:

Custom templates specifically since Woocommerce 3.3
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.
The specific filter hooks related to templates for Woocommerce:
wc_get_template
wc_get_template_part
woocommerce_locate_template
woocommerce_template_path (The theme's override templates folder)
Case 1. Using wc_get_template_part (your case):
If you look at single-product.php template, the content-single-product.php template is loaded with the following at this line:
<?php wc_get_template_part( 'content', 'single-product' ); ?>
So we will use the hook wc_get_template_part.
Let say that 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:
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.
Case 2. Using 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:
add_filter( 'wc_get_template', 'custom_wc_template', 10, 5 );
function custom_wc_template( $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/custom-meta.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.
About overriding woocommerce templates:
Official documentation: Template structure & Overriding templates via a theme
WooCommerce action hooks and overriding templates
Overriding specific third party Woocommerce plugin templates

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;
}

Related

Where to insert php code into the functions.php file of my child theme

First of all, I'm not a developer. I've managed to insert some functions for my WooCommerce webshop by inserting code snippets into the functions.php file of my parent theme. I've made a child theme in case I have to update the theme. I'm afraid these functions get lost otherwise.
I've used the Child Theme Configurator plugin to create the child theme. Now I'm trying to insert the same code snippets into the functions.php file of the child theme. I however, break my site every time. Before adding the code I've removed the functions from the parent theme, otherwise an overlap might excist.
I think I'm adding these code snippets at the wrong location within the file. Below you see the code from the child theme:
<?php
// Exit if accessed directly
if ( !defined( 'ABSPATH' ) ) exit;
// BEGIN ENQUEUE PARENT ACTION
// AUTO GENERATED - Do not modify or remove comment markers above or below:
if ( !function_exists( 'chld_thm_cfg_locale_css' ) ):
function chld_thm_cfg_locale_css( $uri ){
if ( empty( $uri ) && is_rtl() && file_exists( get_template_directory() . '/rtl.css' ) )
$uri = get_template_directory_uri() . '/rtl.css';
return $uri;
}
endif;
add_filter( 'locale_stylesheet_uri', 'chld_thm_cfg_locale_css' );
if ( !function_exists( 'child_theme_configurator_css' ) ):
function child_theme_configurator_css() {
wp_enqueue_style( 'chld_thm_cfg_child', trailingslashit( get_stylesheet_directory_uri() ) . 'style.css', array( 'hello-elementor','hello-elementor','hello-elementor-theme-style' ) );
}
endif;
add_action( 'wp_enqueue_scripts', 'child_theme_configurator_css', 10 );
// END ENQUEUE PARENT ACTION
This is the code I've successfully added to the parent theme. Now I want to add it to the child theme:
add_filter( 'woocommerce_product_tabs', 'woo_remove_product_tabs', 98 );
function woo_remove_product_tabs( $tabs ) {
unset( $tabs['description'] ); // Remove the description tab
unset( $tabs['additional_information'] ); // Remove the additional information tab
return $tabs;
}
/* melding uitschakelen: toegevoegd aan winkelwagen */
add_filter( 'wc_add_to_cart_message_html', 'ql_remove_add_to_cart_message' );
function ql_remove_add_to_cart_message( $message ){
return '';
}
Where and how do I add it?
Thanks for the help in advance! :)

Add a body class for product category archive page in Woocommerce

I want to add product category slug in the body class on product category archive pages.
Here is an example of what I would like (url of the product category page example):
https://example.com/product-category/canon/… So I would like "canon" in the body class.
Your example link doesn't seem to work; however, WooCommerce should already be adding a term-specific class to your category pages: something along the lines of archive tax-product_cat term-{slug} term-{id} to a product category page. Taking your example link, and assuming the term ID is 7 (for example), the body class would include:
tax-product_cat term-7 term-canon
So if you need to access it via CSS/jQuery/whatever, you can use the selector body.term-canon.
You can use page slug in body class, See this code
function add_slug_body_class( $classes ) {
global $post;
if ( isset( $post ) ) {
$classes[] = $post->post_type . '-' . $post->post_name;
}
return $classes;
}
add_filter( 'body_class', 'add_slug_body_class' );
for this you need to add the below code in your functions.php file in your activated theme.
add_filter( 'body_class', 'product_cats_css_body_class' );
function product_cats_css_body_class( $classes ){
if( is_archive( 'product-cat' ) )
{
$custom_terms = get_the_terms(0, 'product_cat');
if ($custom_terms) {
foreach ($custom_terms as $custom_term) {
$classes[] = $custom_term->slug;
}
}
}
return $classes;
}
this will add the category slug at the last of body class.
Even if Woocommerce embed as body class the product category term slug as term-canon, you can easily add just canon using the dedicated WordPress body_class action hook, with Woocommerce is_product_category() conditional function and WordPress get_queried_object() this way:
add_filter( 'body_class', 'product_category_slug_to_body_class', 99, 1 );
function product_category_slug_to_body_class( $classes ){
if( is_product_category() ){
$classes[] = get_queried_object()->slug;
}
return $classes;
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.

Rename My account tabbed menu items in Woocommerce

I am currently using Wordpress Version 4.9.8 and enabled Woocommerce plugin. I need to rename the tab name "Dashboard" to "My Rewards" in My account pages.
I found the code below, but it doesn't seem to be working:
function wpb_woo_endpoint_title( $title, $id ) {
if ( is_wc_endpoint_url( 'dashboard' ) && in_the_loop() ) { // add your endpoint urls
$title = "My Rewards"; // change your entry-title
return $title;
}
add_filter( 'the_title', 'wpb_woo_endpoint_title', 10, 2 );
Any help is appreciated.
Try the following instead (Works in Woocommerce since version 2.6):
add_filter( 'woocommerce_account_menu_items', 'custom_my_account_menu_items', 22, 1 );
function custom_my_account_menu_items( $items ) {
$items['dashboard'] = __("My Rewards", "woocommerce");
return $items;
}
This code goes in function.php file of your active child theme (or active theme). Tested and works.

Add Specific Category Page Templates to WordPress with a Plugin

How do you add a category template via a Wordpress plugin. It needs to be for a specific category. So if the category is 'category-awesome' load 'category-awesome.php'. I've come across using the single template hook e.g:
add_filter('single_template', 'my_custom_template');
But haven't found anything for categories or a single category.
Many Thanks
Think I solved it. Use the template include filter hook:
function category_awesome( $template ) {
if (is_category( 'awesome' )) {
$new_template = dirname(__FILE__) . '/custom-template-awesome.php';
if ( '' != $new_template ) {
return $new_template ;
}
}
return $template;
}
add_filter( 'template_include', 'category_awesome' );

WordPress - Ovveride plugin template in theme

I have plugin that create new post type. Also plugin set single template for it's single page.
add_filter( 'single_template', array( &$this, 'register_ipa_product_post_type_single_template' ) );
function register_ipa_product_post_type_single_template( $single_template ) {
global $post;
if ( $post->post_type == 'product' ) {
$single_template = IPA_PRODUCT_POST_TYPE_TEMPLATE_FOLDER . 'single-product.php';
}
return $single_template;
}
How i can override single-product.php in my theme.
I don't found any solutions for my question at this site.
just filter it a little later than the current function (ps if doing this within a class you need to reference it using array(&$this, 'function'). I left it out as i assume you are using the functions.php or function override....etc
add_filter( 'single_template', 'register_ipa_product_post_type_single_template', 100 );
function change_temp( $single_template ) {
global $post;
if ( $post->post_type == 'product' ) {
$single_template = 'path to your template file';
}
return $single_template;
};

Categories