Comments on: Override WooCommerce template from your plugin https://pluginrepublic.com/override-woocommerce-template-plugin/ WooCommerce Plugins Thu, 01 Nov 2018 11:14:21 +0000 hourly 1 By: Ömür Yanıkoğlu https://pluginrepublic.com/override-woocommerce-template-plugin/#comment-14243 Thu, 01 Nov 2018 11:14:21 +0000 https://pluginrepublic.com/?p=15658#comment-14243 Thank you!
This helps me a lot.

I found a generic way for my project:


/**
* Plugin dir
*/
define('IRM_WOOCOMMERCE_VARIATION_SWATCHER_DIR', plugin_dir_path( __FILE__ ));

/**
* Override WooCommerce templates from your plugin with child theme way
*/
function irm_woo_locate_template( $template, $template_name, $template_path ) {
$re = '/woocommerce\/(templates\/)?(.*)/m';
preg_match($re, $template, $matches);
if(isset($matches[2]) && !empty($matches[2]) && file_exists( IRM_WOOCOMMERCE_VARIATION_SWATCHER_DIR . 'woocommerce/' . $matches[2] )) {
$template = IRM_WOOCOMMERCE_VARIATION_SWATCHER_DIR . 'woocommerce/' . $matches[2];
}
return $template;
}
add_filter( 'woocommerce_locate_template', 'irm_woo_locate_template', 10, 3 );

After that, you could use [the official way](https://docs.woocommerce.com/document/template-structure/).

Of course your way more optimal for the best performance.
No doubt of that.

]]>