How to override woocommerce product-image template - php

I want to use my own custom template file (with a custom name) for product image and gallery thumbnails, so need to override the default Woocommerce product-image template by using this:
add_filter( 'wc_get_template', 'modify_product_gallery_template', 10, 5 );
function modify_product_gallery_template( $located, $template_name, $args, $template_path, $default_path ) {
if ( 'single-product/product-image.php' == $template_name ) {
$located = '... my-gallery.include.php';
}
return $located;
}
But no success yet. Is there any other way for doing this?

It's a problem of path that has to be the absolute server path using get_theme_file_path() for example.
Assuming that your custom template file is named my-gallery.include.php and if:
1) it's located in your active theme's root directory you will use:
$located = get_theme_file_path('/my-gallery.include.php');
So in your code:
add_filter( 'wc_get_template', 'modify_product_gallery_template', 10, 5 );
function modify_product_gallery_template( $located, $template_name, $args, $template_path, $default_path ) {
if ( 'single-product/product-image.php' == $template_name ) {
$located = get_theme_file_path('/my-gallery.include.php');
}
return $located;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
2) it's located inside a your active theme on a woocommerce subfolder, you will use:
$located = get_theme_file_path('/woocommerce/my-gallery.include.php');
3) If you want to override the template via your active theme, just copy the file from the woocommerce plugin to: woocommerce/single-product/product-image.php…
Now you can open edit the copied template and make changes without your actual code and the changes will appear once saved.

To override any woocommerce template you don't need to write any code. You can do is simply creating that file inside your active theme. For Example in your case you want to overwrite 'product-image.php' which is located inside
wp-content/plugins/woocommerce/templates/single-product/product-image.php
So for that what you can do, you can just create that template inside your path and for that you need to create the path to the template inside the theme. Like in your case create the path to the template i.e :
woocommerce -> templates -> single-product -> product-image.php
When woocommerce runs it will take the template from the theme if available else from the plugin.
Hope this make sense.

You can override woocommerce product-image template
project/wp-content/plugins/woocommerce/templates/single-product/product-image.php
Create below folder structure and copy above page and do modification as per your requirement
project/wp-content/themes/yourtheme/woocommerce/templates/single-product/product-image.php

Related

Woocommerce conditionally load a custom product template

I am creating a custom theme and I have copied the file content-product.php to my theme folder. There I have made changes to the html structure and css. I can load of list of products and see the changes working via the short code [products].
However elsewhere on the site I want to display another list of products but from a different category. I would use the shortcode [products category="special category"] These products should be displayed using a different template.
My question is: Where can I inspect the shortcode query? and how can I conditionally load a different template depending on which products are being displayed?
In my themes functions.php file I have started to extend the [products] shortcode like this:
function my_wc_shortcode_product_query_args($args, $atts){
if ( isset( $args['category'] ) && $args['category'] == "Daoine Óga") {
var_dump($args);
// Tell Woocommerce to load my custom template instead of the my-theme/woocommerce/content-product.php
}
return $args;
}
But I'm not sure how I can return or get woocommerce to display my custom template at this point.
The woocommerce file that creates the [products] shortcode can be found at plugins/woocommerce/includes/shortcodes/class-wc-shortcode-products.php
Make a folder in plugins folder called custom-product-templates and make a copy of the woocommerce file class-wc-shortcode-products.php to that folder.
Add the plugin comments to the top of that file:
/*
Plugin name: Custom Product Template
Plugin URI: https://anirishwebdesigner.com
Description: Display custom product templates in woocommerce loop
Author: Linda Keating
Author URI: http://anirishwebdesigner.com
Version: 1.0
*/
Navigate to localhost/wp-admin in browser and to the plugins and activate newly created plugin
I can now safely edit that file so that the shortcode behaviour loads the template I want based on certain criteria.
First extend the class so that it accepts a template attribute
In the parse attributes function add
'template' => '',
Then search for wc_get_template_part('content', 'product'); and modify the code here to load different templates in an if..else statement. Something like:
`if($this->attributes['template'] == 'Daoine Óga') {
wc_get_template_part( 'content', 'childrenproduct' );
} else {
wc_get_template_part( 'content', 'product' );
}`
If there are simpler / better solutions please let me know.

How to get child theme module templates to override main module template

For some reason my child theme templates are not being recognized.
I believe I have followed the correct procedure (and checked for caching etc)
/wp-content/themes/divi-child/includes/builder/module/Blog.php
should replace
/wp-content/themes/Divi/includes/builder/module/Blog.php
(same path and same file with a slight update)
The child theme module template is not recognized (changes to the child theme template have no effect)
I have tested editing the main file and this works immediately every
time.
Any advice greatly appreciated.
Cheers
EDIT
The below should work according to Divi however it breaks the site when I try.
Apparently it is not enough to just copy the module into the child theme. The file needs to be duplicated. Then copied file to child-theme/custom-modules/Blog.php. After adding following code to the bottom of the functions.php file:
function divi_custom_blog_module() {
get_template_part( '/custom-modules/Blog' );
$myblog = new custom_ET_Builder_Module_Blog();
remove_shortcode( 'et_pb_blog' );
add_shortcode( 'et_pb_blog', array( $myblog, '_render' ) );
}
add_action( 'et_builder_ready', 'divi_custom_blog_module' );
There are a few other steps https://intercom.help/elegantthemes/en/articles/4532734-moving-blog-module-in-child-theme
Create a new folder in the child theme folder, for example, includes folder.
Now copy the Divi/includes/builder/module/Blog.php file from the parent theme into the child-theme/includes/ folder.
Open up the Blog.php file of your child theme and replace this line (at the very top):
require_once 'helpers/Overlay.php';
class ET_Builder_Module_Blog extends ET_Builder_Module_Type_PostBased {
with:
get_template_part( '/includes/builder/module/helpers/Overlay.php' );
class custom_ET_Builder_Module_Blog extends ET_Builder_Module_Type_PostBased {
Replace: $this->vb_support = 'on'; with $this->vb_support = 'off';
Remove this line from the bottom: new ET_Builder_Module_Blog();
Finally, add the following code to the functions.php file in your child theme folder:
/*================================================
#Load custom Blog Module
================================================*/
function divi_custom_blog_module() {
get_template_part( '/includes/Blog' );
$myblog = new custom_ET_Builder_Module_Blog();
remove_shortcode( 'et_pb_blog' );
add_shortcode( 'et_pb_blog', array( $myblog, '_render' ) );
}
add_action( 'et_builder_ready', 'divi_custom_blog_module' );
I have got this issue similar to this before. I think - maybe the blog template is called by another file in the "father theme" folder some thing like:
$url = dirname(__FILE__)."/includes/builder/module/Blog.php";
this will cause the issue. to fix this there are two way:
#1: find and edit the file wich is call the blog template (not recommend)
#2: find and copy the file wich is call the blog template to your child theme (you can try with index.php, content.php or single.php first) (recommend)

How to Disable Changing the Installed Theme in Wordpress?

I developed my own WordPress theme for my client!.
If he changes the theme from outside, he will lose my theme.
I want to disable him from changing Wordpress theme from mine theme.
How can I Disable it? is there any way by editing the Wordpress files?
You can remove theme submenu using following code. Add below code in your theme's functions.php file.
add_action( 'admin_menu', 'adjust_the_wp_menu', 999 );
function adjust_the_wp_menu()
{
remove_submenu_page( 'themes.php', 'themes.php' );
}
Restrict admin to open file
add_action( 'current_screen', 'this_screen' );
function this_screen()
{
$current_screen = get_current_screen();
if( $current_screen ->id === "themes" )
{
wp_die("You don't have access to this page.");
}
}

How to localize child theme with Loco Translate

I am getting my head around adding localizable strings to a wordpress child theme, and have not been able to do this successfully.
I have a child theme with a .php page to which I want to add a localizable string. In my child theme's functions.php, I have added the following line:
load_theme_textdomain( 'i-craft-child', get_template_directory() . '/languages' );
Next, using Loco Translate, I uploaded the files de_DE.po and de_DE.mo to the directory /languages within the child theme directory.
Finally, I added the following line to my html page:
<span><small>><?php _e( 'Your email address is also your username and it cannot be changed', 'i-craft-child' ); ?></small></span>
However, the span above is displayed in English (instead of German). I am not sure where in the localization process am I failing and would appreciate any pointers to solve this problem.
Make sure that your theme text-domain is included, you can use something like this (if file doesn't exists, it will break your site, so use it on test environment).
$loaded = load_child_theme_textdomain( 'i-craft-child', get_template_directory() . '/languages' );
if( ! $loaded ) {
echo 'Unable to load files';
die;
}
Also, did you specify WP_LANG in wp-config.php, like this?
define('WP_LANG', 'de_DE'); // for example
Following up on the pointer from unixarmy above, the problem was that the function load_child_theme_textdomain was not able to read the files, due to me using the function get_template_directory() as a parameter. get_template_directory() will return the path of the parent theme, not the child. Substituting that for get_stylesheet_directory() solved the problem.
I achieved that problem going to the Advanced configuration inside Loco:
Themes -> Child Theme Name -> Advanced.
Inside this area you can define the 'text domain' you will use when translating your theme, for example:
<?php _e('My string','generatepress-child'); ?>
Where generatepress-child is the text domain.
In my case the child theme and parent theme where merging on the same path (on the parent path) and loco wasn't able to 'fetch / scan' the strings inside the child theme, so I added manually the new child path inside this advanced section and now I can translate the strings inside my child theme.
After this I have had to add this lines on my functions.php parent theme:
function wpdocs_child_theme_setup() {
load_child_theme_textdomain( 'generatepress-child', get_stylesheet_directory() );
}
add_action( 'after_setup_theme', 'wpdocs_child_theme_setup' );
Hope it helps!

Woocommerce archive-product.php overwrite hack not working

I've tried this hack from a previous thread:
Replace woocommerce_content() in woocommerce.php with:
if ( is_singular( 'product' ) ) {
woocommerce_content();
} else {
//For ANY product archive.
//Product taxonomy, product search or /shop landing
woocommerce_get_template( 'archive-product.php' );
}
This now loads the archive-product.php from the /plugin folder which is a step forward, but it should really load the archive-product.php from the /myTheme folder. I don't want to mess around in the plugin folder for obvious reasons...
Has anyone found a workaround for this?
Your archive-product.php file should be in your theme folder, within a /woocommerce directory. So: /wp-content/themes/your-theme/woocommerce/archive-product.php. It's a bit confusing, because the actual WooCommerce templates are within an additional /templates directory, in the plugin itself.
Read more about overriding WooCommerce templates in their documentation: http://docs.woothemes.com/document/template-structure/

Categories