Woocommerce archive-product.php overwrite hack not working - php

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/

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 override woocommerce product-image template

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

Usage of Woocommerce's template system

I am trying to use Woocommerce's wc_get_template function to load my own templates, but it dosen't seem to work (it's not outputting content from the template files), together with my Woocommerce plugin. Below is what I have tried:
plugindir/templates/testing-template.php:
<h1>Output stuff</h1>
plugindir/pluginname.php:
if(in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option('active_plugins')))) {
function template_loader(){
wc_get_template('testing-template.php', array());
}
add_action('init', 'template_loader');
}
wc_get_template will first try to load the template from the the theme in the woocommerce folder. If it does not find the template in the theme's woocommerce folder it will try and load the template from woocommerce templates folder.
wc_get_tempalte will not just work with any template you want. If you are building a plugin of your own you can try and look at the WC_Template Class and try to mitigate the functionality of wc_template in your own plugin with plugin_prefix_template() and also have a templates folder in your plugin.
https://docs.woocommerce.com/wc-apidocs/source-class-WC_Template_Loader.html#13-510

Woocommerce shop page custom template

As I understand by default Woocommerce shop page uses product archive template. What I am looking for, is to use a custom template for shop page.
Here is what I did:
Create template "my-shop"
Create page "My shop" -> choose template "my-shop"
Choose "My shop" as Woocommerce shop page
But none of the changes I made to "my-shop" template are present on the shop page.
What am I missing here? I would not like to change product archive itself, just the shop page.
Is there a way to disable product archive from being a default for shop page?
Thanks
I know it's too late and you may have figured it out by now. In any case, the changes that you would like to make to the WooCommerce Shop Page need to be done in the archive-product.php and it would be safer to create a child theme and do these changes. Making enhancements and customizations in a Child theme is best practice so that you can update the parent theme at any time and it won't affect your store.
I hope this helps, for more information on how you can use WooCommerce short-codes to customize your store can be found here.
To add to Silver Ringvee's answer - he used is_page but that only works on wordpress pages. For woocommerce you need to use something like is_woocommerce() . See Woocommerce conditional tags page.
My example code uses the is_shop conditional tag as that was the page you wanted to change. the code get_template_part( 'content', 'shop' ); will call the file content-shop.php in your theme root folder. This code is to be added at the top of wp-content\themes\*theme*\woocommerce\archive-product.php that you can copy from wp-content\plugins\woocommerce\templates\archive-product.php
You can add it just before get_header( 'shop' ); line 23 in my file - and the entire page will be drawn from your template. If you want to keep the header of the shop page, then put this code after the get_header code. Remember to include a footer in your file as well
if (is_shop()) {
get_template_part( 'content', 'shop' );
} else {
#normal archive-product code here
}
The solution (not perfect) that I figured to work best, until someone finds a way to actually change the template from dashboard:
Adding:
<?php
if (is_page( 'Page Title' ) ):
# Do your stuff
endif;
?>
to content-product.php in my theme's woocommerce folder.
If you prefer to go with code, you can create a redirect from the original shop page to your page via wp_redirect.
add_action('template_redirect', 'bc_010101_redirect_woo_pages');
function bc_010101_redirect_woo_pages()
{
if (is_shop())
{
wp_redirect('your_shop_url_here');
exit;
}
}
More detailed tutorial can be found here
This is not possible to create custom template for shop page , just copy and paste woocommerce Templates into you theme folder and Try to work in content-product.php template .

Categories