I need to override all of child theme templates woocommerce and parent templates in a plugin. Basically, I'm making couple of copies to my site that has some custom functionalities added via the child theme. So, in order to update all of the sites at once I want to use a plugin instead of a child theme. Because I assume it's pretty difficult to make child theme do automatically updates. (maybe I'm wrong in this)
I'm using a parent theme that has different versions of woocommerce templates. For example, child-theme/woocommerce/cart/cart-v2.php is a cart template.
This is what I use to override woocommerce templates, but they override woocommerce plugin templates not parent theme templates.
add_filter( 'woocommerce_locate_template', 'woo_adon_plugin_template', 1, 3 );
function woo_adon_plugin_template( $template, $template_name, $template_path ) {
global $woocommerce;
$_template = $template;
if ( ! $template_path )
$template_path = $woocommerce->template_url;
$plugin_path = RCW2P_PLUGIN_PATH . '/templates/woocommerce/';
if(file_exists( $plugin_path . $template_name ))
$template = $plugin_path . $template_name;
if( ! $template )
$template = locate_template(
array(
$template_path . $template_name,
$template_name
)
);
if( ! $template && file_exists( $plugin_path . $template_name ) )
$template = $plugin_path . $template_name;
if ( ! $template )
$template = $_template;
return $template;
}
With this code I have to have this path for cart page plugin/templates/woocommerce/cart/cart.php
Also, this code doesn't overrides templates files that are in woocommerce folder, such as taxonomy-product-cat.php
Any help is much appreciated.
Related
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! :)
In my plug-in i'm trying to override part of the theme's template with a template of my own but I'm struggling to get it to work.
add_filter( 'template_include', 'demo_page_template');
function demo_page_template($template) {
if ( is_product() ) {
$template = untrailingslashit( plugin_dir_path( __FILE__ ) ) . 'templates/woocommerce/single-product.php';
}
return $template;
}
I have added the appropriate files and folders but I'm not getting any reaction.
thanks for reading, appreciate any help I can get.
I have several Wordpress sites set up and have child themes created for all of them. One of the sites is going to have multiple subsites that will all include customized branding. I have a style sheet enqueued in my child theme with the following code in my functions.php file.
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( 'chld_thm_cfg_parent_css' ) ):
function chld_thm_cfg_parent_css() {
wp_enqueue_style( 'chld_thm_cfg_parent', trailingslashit( get_template_directory_uri() ) . 'style.css', array( 'font-awesome','swiper','perfect-scrollbar','animate' ) );
}
endif;
add_action( 'wp_enqueue_scripts', 'chld_thm_cfg_parent_css', 10 );
This works fine and my style sheet is functioning as expected. The issue I'm running in to is as I add new subsites with customized branding, the size of this style sheet, style.css, is getting harder to manage. What I want to do is enqueue another style sheet in my child theme for the subsites. I tried to replicate the code above to enqueue the new style sheet, but it does not work. Here is what I added in my functions.php file immediately below the code above:
if ( !function_exists( 'coastal_thm_cfg_locale_css' ) ):
function coastal_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', 'coastal_thm_cfg_locale_css' );
if ( !function_exists( 'coastal_thm_cfg_parent_css' ) ):
function coastal_thm_cfg_parent_css() {
wp_enqueue_style( 'coastal_thm_cfg_parent', trailingslashit( get_template_directory_uri() ) . 'coastal-style.css', array( 'font-awesome','swiper','perfect-scrollbar','animate' ) );
}
endif;
add_action( 'wp_enqueue_scripts', 'coastal_thm_cfg_parent_css', 11 );
I thought this would work but it does not. I understand child themes but must admin the whole enqueuing process is quite confusing to me. I used a plugin, Child Theme Configurator, to enqueue the primary child theme style sheet.
I've looked through several blog posts and questions in Stackoverflow, but cannot find an answer to the specific problem.
Is enqueuing multiple style sheets like this possible?
If so, what am I doing wrong?
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;
}
WooCommerce provides various hooks and action to change the predefined templates. But i need to hide the shipping address from order emails and display N/A. I have created a custom shipping method using plugin. Using that plugin i have to changed the address.
Any help will be appreciated.
Finally, i found a solution :
function myplugin_plugin_path() {
return untrailingslashit( plugin_dir_path( __FILE__ ) );
}
add_filter( 'woocommerce_locate_template', 'myplugin_woocommerce_locate_template', 10, 3 );
function myplugin_woocommerce_locate_template( $template, $template_name, $template_path ) {
global $woocommerce;
$_template = $template;
if ( ! $template_path ) $template_path = $woocommerce->template_url;
$plugin_path = myplugin_plugin_path() . '/woocommerce/';
$template = locate_template( array(
$template_path . $template_name,
$template_name));
// Modification: Get the template from this plugin, if it exists
if ( ! $template && file_exists( $plugin_path . $template_name ) )
$template = $plugin_path . $template_name;
// Use default template
if ( ! $template )
$template = $_template;
return $template;
}
The normal WooCommerce template loader searches the following locations in order:
your theme / template path / template name
your theme / template name
your plugin / woo-commerce / template name
default path / template name
Reference Link : https://www.skyverge.com/blog/override-woocommerce-template-file-within-a-plugin/