Override WooCommerce templates from your plugin with child theme way

I needed this for one of my clients and this article helps me to find the right solution.

You can use the following snippet and after that you only need to place your template files in the root of your plugin like the official way.

/**
 * 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 );

Hope this helps someone.