Skip to content

Instantly share code, notes, and snippets.

@JulienMelissas
Last active June 5, 2023 01:13
Show Gist options
  • Save JulienMelissas/d3601af950221432a109c8c4b1617b87 to your computer and use it in GitHub Desktop.
Save JulienMelissas/d3601af950221432a109c8c4b1617b87 to your computer and use it in GitHub Desktop.
<?php
/*
Plugin Name: Fix WooCommerce Template Cache
Plugin URI: https://woocommerce.com
Description: This fixes some issues with WC's template caching on sites that use multiple containers, due to the entire path being cached. If the file does not exist, this script uses default WC logic (copy/pasted) to grab the correct templates.
Version: 1.0.0
Author: Julien Melissas
Author URI: https://julienmelissas.com
*/
add_filter('wc_get_template_part', function( $template, $slug, $name ) {
// If the template does not exist...
if ( ! file_exists( $template ) ) {
// Reset $template variable
$template = false;
if ( $name ) {
$template = WC_TEMPLATE_DEBUG_MODE ? '' : locate_template(
array(
"{$slug}-{$name}.php",
WC()->template_path() . "{$slug}-{$name}.php",
)
);
if ( ! $template ) {
$fallback = WC()->plugin_path() . "/templates/{$slug}-{$name}.php";
$template = file_exists( $fallback ) ? $fallback : '';
}
}
if ( ! $template ) {
// If template file doesn't exist, look in yourtheme/slug.php and yourtheme/woocommerce/slug.php.
$template = WC_TEMPLATE_DEBUG_MODE ? '' : locate_template(
array(
"{$slug}.php",
WC()->template_path() . "{$slug}.php",
)
);
}
}
return $template;
}, 3, 99);
add_filter('wc_get_template', function( $template, $template_name, $args, $template_path, $default_path ) {
// If the template does not exist...
if ( ! file_exists( $template ) ) {
$template = wc_locate_template( $template_name, $template_path, $default_path );
}
return $template;
}, 5, 99);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment