Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save plugin-republic/f3b4f5eae0188b74894416d8b03d0d8b to your computer and use it in GitHub Desktop.
Save plugin-republic/f3b4f5eae0188b74894416d8b03d0d8b to your computer and use it in GitHub Desktop.
/**
* Filter the cart template path to use our cart.php template instead of the theme's
*/
function csp_locate_template( $template, $template_name, $template_path ) {
$basename = basename( $template );
if( $basename == 'cart.php' ) {
$template = trailingslashit( plugin_dir_path( __FILE__ ) ) . 'templates/cart.php';
}
return $template;
}
add_filter( 'woocommerce_locate_template', 'csp_locate_template', 10, 3 );
@hayatbiralem
Copy link

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.

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment