I've read a bunch of articles about overriding the default /woocommerce/ templates and tried implementing the following (which were the best/most relevant that I could find all to no avail):
- Load WooCommerce templates from my plugin folder first
- Override WooCommerce Template File Within a Plugin
Essentially, what I would like to accomplish is: load all template files (for archives and posts not just template parts) from ~/wp-content/my-plugin/templates/woocommerce/* UNLESS the files are in my theme (and I don't have to override each file instance in my function) . For instance:
~/wp-content/my-plugin/templates/woocommerce/single-product.php
(this seems like it just doesn't want to load via plugin)~/wp-content/my-plugin/templates/woocommerce/archive-products.php
(this seems like it just doesn't want to load via plugin)~/wp-content/my-plugin/templates/woocommerce-pdf-invoices-packing-slips/*
(I also want to be able to override other plugin extension templates just like I would be able to in my child theme)
EDIT:
The friendly folks at SkyVerge sent me the following code, which I tested and confirm that it works for template parts.
// Locate the template in a plugin
function myplugin_woocommerce_locate_template( $template, $template_name, $template_path ) {
$_template = $template;
if ( ! $template_path ) {
$template_path = WC()->template_path();
}
$plugin_path = myplugin_plugin_path() . '/templates/';
// Look within passed path within the theme - this is priority
$template = locate_template(
array(
trailingslashit( $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;
}
add_filter( 'woocommerce_locate_template', 'myplugin_woocommerce_locate_template', 10, 3 );
// Helper to get the plugin's path on the server
function myplugin_plugin_path() {
// gets the absolute path to this plugin directory
return untrailingslashit( plugin_dir_path( __FILE__ ) );
}
The above code works for something like:
~/myplugin/templates/single-product/product-image.php
But does NOT work for:
~/myplugin/templates/single-product.php
Where I'm getting stuck:
- There are solutions to override bits and pieces of WC templates, but there I've not found / been able to create a solution that does comprehensive overrides (i.e. overriding ability just like a child theme would)
- I can't seem to find the right combination of filter hooks; single-product.php and archive-product.php seem to be controlled by functions outside the standard WC template functions
Thanks in advance!