Skip to content

Instantly share code, notes, and snippets.

@danchivz
Created April 9, 2015 18:52
Show Gist options
  • Save danchivz/f552916cee702c9d6d45 to your computer and use it in GitHub Desktop.
Save danchivz/f552916cee702c9d6d45 to your computer and use it in GitHub Desktop.
Override WooCommerce template files via a plugin.
<?php
/**
* WooCommerce Plugin Template Loader
* Override WooCommerce template files within a custom plugin folder
* http://gist.github.com/danchivz
*
* Licensed under the MIT license.
*/
/**
* To override templates within a plugin, two filters are provided by WooCommerce, ' woocommerce_locate_template' and 'wc_get_template_part'.
* Create a subfolder named 'woocommerce' inside the plugin folder, and place there custom templates.
* Templates will be loaded in the following hierarchy:
* theme/template_path/template_name
* theme/template_name
* plugin/template_path/template_name
* default/template_name
*/
// get path for templates used in loop
add_filter( 'wc_get_template_part', function( $template, $slug, $name )
{
// look in plugin/woocommerce/slug-name.php or plugin/woocommerce/slug.php
if ( $name ) {
$path = plugin_dir_path( __FILE__ ) . WC()->template_path() . "{$slug}-{$name}.php";
} else {
$path = plugin_dir_path( __FILE__ ) . WC()->template_path() . "{$slug}.php";
}
return file_exists( $path ) ? $path : $template;
}, 10, 3 );
// get path for all other templates.
add_filter( 'woocommerce_locate_template', function( $template, $template_name, $template_path )
{
$path = plugin_dir_path( __FILE__ ) . $template_path . $template_name;
return file_exists( $path ) ? $path : $template;
}, 10, 3 );
?>
@KirillFE
Copy link

stackoverflow.com/a/35252520/4689173 this works for single product.php

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment