I found this article on using plugins instead of themes to override a Woocommerce template:
https://www.skyverge.com/blog/override-woocommerce-template-file-within-a-plugin/
It seems to almost work, but it’s not working for any templates that are in the root of the woocommerce/templates directory (like single-product.php or content-single-product.php).
To illustrate, I have this filter:
add_filter('woocommerce_locate_template', array($this, 'woocommerce_locate_template'), 10, 3);
calling this function:
public function woocommerce_locate_template($template, $template_name, $template_path) {
global $woocommerce;
al_log('Looking for ' . $template . ', ' . $template_name . ', ' . $template_path);
$_template = $template;
if (! $template_path) $template_path = $woocommerce->template_url;
$plugin_path = $this->my_plugin_path() .'/woocommerce/';
//look within passed path within the theme. This is the priority
$template = locate_template(
array(
$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;
}
It results in these log lines:
[davetbo@dev wp-content]$ [05-Feb-2016 17:10:46 UTC] Looking for /var/www/html/wp-content/plugins/woocommerce/templates/global/breadcrumb.php, global/breadcrumb.php, woocommerce/
[05-Feb-2016 17:10:47 UTC] Looking for /var/www/html/wp-content/plugins/woocommerce/templates/single-product/sale-flash.php, single-product/sale-flash.php, woocommerce/
[05-Feb-2016 17:10:47 UTC] Looking for /var/www/html/wp-content/plugins/woocommerce/templates/single-product/product-image.php, single-product/product-image.php, woocommerce/
[05-Feb-2016 17:10:47 UTC] Looking for /var/www/html/wp-content/plugins/woocommerce/templates/single-product/product-thumbnails.php, single-product/product-thumbnails.php, woocommerce/
[05-Feb-2016 17:10:47 UTC] Looking for /var/www/html/wp-content/plugins/woocommerce/templates/single-product/title.php, single-product/title.php, woocommerce/
[05-Feb-2016 17:10:47 UTC] Looking for /var/www/html/wp-content/plugins/woocommerce/templates/single-product/rating.php, single-product/rating.php, woocommerce/
[05-Feb-2016 17:10:47 UTC] Looking for /var/www/html/wp-content/plugins/woocommerce/templates/single-product/price.php, single-product/price.php, woocommerce/
[05-Feb-2016 17:10:47 UTC] Looking for /var/www/html/wp-content/plugins/woocommerce/templates/single-product/short-description.php, single-product/short-description.php, woocommerce/
[05-Feb-2016 17:10:47 UTC] Looking for /var/www/html/wp-content/plugins/woocommerce/templates/single-product/meta.php, single-product/meta.php, woocommerce/
[05-Feb-2016 17:10:47 UTC] Looking for /var/www/html/wp-content/plugins/woocommerce/templates/single-product/share.php, single-product/share.php, woocommerce/
[05-Feb-2016 17:10:47 UTC] Looking for /var/www/html/wp-content/plugins/woocommerce/templates/single-product/tabs/tabs.php, single-product/tabs/tabs.php, woocommerce/
[05-Feb-2016 17:10:47 UTC] Looking for /var/www/html/wp-content/plugins/woocommerce/templates/single-product/tabs/description.php, single-product/tabs/description.php, woocommerce/
[05-Feb-2016 17:10:47 UTC] Looking for /var/www/html/wp-content/plugins/woocommerce/templates/single-product/tabs/additional-information.php, single-product/tabs/additional-information.php, woocommerce/
[05-Feb-2016 17:10:47 UTC] Looking for /var/www/html/wp-content/plugins/woocommerce/templates/single-product/product-attributes.php, single-product/product-attributes.php, woocommerce/
[05-Feb-2016 17:10:47 UTC] Looking for /var/www/html/wp-content/plugins/woocommerce/templates/single-product/up-sells.php, single-product/up-sells.php, woocommerce/
[05-Feb-2016 17:10:47 UTC] Looking for /var/www/html/wp-content/plugins/woocommerce/templates/single-product/related.php, single-product/related.php, woocommerce/
[05-Feb-2016 17:10:47 UTC] Looking for /var/www/html/wp-content/plugins/woocommerce/templates/global/sidebar.php, global/sidebar.php, woocommerce/
As you can see, it never goes looking for anything in the root of the woocommerce/templates directory. Any thoughts as to why that filter doesn’t work for single-product.php or content-single-product.php?
I currently have a woocommerce folder in the root of my plugin folder and inside that there’s a single-product.php file with a die statement near the top, so I should immediately see if it’s working. I did put the same single-product.php file in a woocommerce folder in my theme and it works there, but not from the plugin. Is there a different hook that I should be using? Please advise.