Overriding template in woocommerce - php

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.

Related

Override all child theme templates with a plugin

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.

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! :)

Replace get_header template from plugin

Im creating a plugin what overrides templates which included in plugin folder. First of all Im trying to override header.php and footer.php files. Tried to create various solutions, but any of them doesnt work. (This is one of the plugin solutions)
Tried to remove get_header hook and add like new, but this doesnt help.
There is some of the latest solutions, what I tried
function header_override () {
// $template = locate_template('header.php');
// load_template(plugin_dir_path( __FILE__ ) . 'templates/header.php');
// print_r($template);
if ( $overridden_template = locate_template( 'header.php' ) ) {
load_template( $overridden_template );
}else{
load_template(plugin_dir_path( __FILE__ ) . 'templates/header.php');
}
}
add_action('template_include', 'header_override');
function new_header_output() {
remove_action( 'get_header', 'header_output', 20);
load_template(plugin_dir_path( __FILE__ ) . 'templates/header.php');
}
add_action('get_header', 'new_header_output');
function action_function_name_391( $name, $args ){
print_r("custom header");
}
add_action( 'get_header', 'action_function_name_391', 10, 2 );
Maybe someone has a similar problem to override theme templates
add_action('get_header', 'replace_header');
function replace_header(){
require PLUGIN_DIR.'includes/templates/header.php';
$templates = [];
$templates[] = 'header.php';
remove_all_actions( 'wp_head' );
ob_start();
locate_template( $templates, true );
ob_get_clean();
}

WooCommerce - How can we change Shipping Address to N/A in order emails?

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/

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' );

Categories