Reputation: 1473
I am developing a WooCommerce plugin (actually usual WP plugin, but works only when WooCommerce enabled), which should to change standard WooCommerce output logic. In particular I need to override standard archive-product.php template by my own. I have found there is no problem to change template in theme, but can not how to do it in the plugin. How I can do it without any changes in WP & WooCommerce cores?
Upvotes: 0
Views: 3571
Reputation: 410
Here is I try something like this. Hope it will help.
Add this filter into your plugin:
add_filter( 'template_include', 'my_include_template_function' );
And then the callback function will be
function my_include_template_function( $template_path ) {
if ( is_single() && get_post_type() == 'product' ) {
// checks if the file exists in the theme first,
// otherwise serve the file from the plugin
if ( $theme_file = locate_template( array ( 'single-product.php' ) ) ) {
$template_path = $theme_file;
} else {
$template_path = PLUGIN_TEMPLATE_PATH . 'single-product.php';
}
} elseif ( is_product_taxonomy() ) {
if ( is_tax( 'product_cat' ) ) {
// checks if the file exists in the theme first,
// otherwise serve the file from the plugin
if ( $theme_file = locate_template( array ( 'taxonomy-product_cat.php' ) ) ) {
$template_path = $theme_file;
} else {
$template_path = PLUGIN_TEMPLATE_PATH . 'taxonomy-product_cat.php';
}
} else {
// checks if the file exists in the theme first,
// otherwise serve the file from the plugin
if ( $theme_file = locate_template( array ( 'archive-product.php' ) ) ) {
$template_path = $theme_file;
} else {
$template_path = PLUGIN_TEMPLATE_PATH . 'archive-product.php';
}
}
} elseif ( is_archive() && get_post_type() == 'product' ) {
// checks if the file exists in the theme first,
// otherwise serve the file from the plugin
if ( $theme_file = locate_template( array ( 'archive-product.php' ) ) ) {
$template_path = $theme_file;
} else {
$template_path = PLUGIN_TEMPLATE_PATH . 'archive-product.php';
}
}
return $template_path;
}
I check this for theme first loading. If the file is not found in theme then it will load from the plugin.
You can change the logic here.
Hope it will do your work.
Thanks
Upvotes: 0
Reputation: 1626
I think you need to do it via hooks (filters & actions) available for WooCommerce.
Here is a list: http://docs.woothemes.com/document/hooks/#templatehooks
Here is where to get started on hooks: http://wp.tutsplus.com/tutorials/the-beginners-guide-to-wordpress-actions-and-filters/
Upvotes: 2