Created
May 4, 2017 07:26
-
-
Save maheshwaghmare/5ef175fb51333b3b11cbfb7746c44e32 to your computer and use it in GitHub Desktop.
Override default WooCommerce templates and template parts from plugin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Override default WooCommerce templates and template parts from plugin. | |
* | |
* E.g. | |
* Override template 'woocommerce/loop/result-count.php' with 'my-plugin/woocommerce/loop/result-count.php'. | |
* Override template part 'woocommerce/content-product.php' with 'my-plugin/woocommerce/content-product.php'. | |
* | |
* Note: We used folder name 'woocommerce' in plugin to override all woocommerce templates and template parts. | |
* You can change it as per your requirement. | |
*/ | |
// Override Template Part's. | |
add_filter( 'wc_get_template_part', 'override_woocommerce_template_part', 10, 3 ); | |
// Override Template's. | |
add_filter( 'woocommerce_locate_template', 'override_woocommerce_template', 10, 3 ); | |
/** | |
* Template Part's | |
* | |
* @param string $template Default template file path. | |
* @param string $slug Template file slug. | |
* @param string $name Template file name. | |
* @return string Return the template part from plugin. | |
*/ | |
function override_woocommerce_template_part( $template, $slug, $name ) { | |
// UNCOMMENT FOR @DEBUGGING | |
// echo '<pre>'; | |
// echo 'template: ' . $template . '<br/>'; | |
// echo 'slug: ' . $slug . '<br/>'; | |
// echo 'name: ' . $name . '<br/>'; | |
// echo '</pre>'; | |
// Template directory. | |
// E.g. /wp-content/plugins/my-plugin/woocommerce/ | |
$template_directory = untrailingslashit( plugin_dir_path( __FILE__ ) ) . 'woocommerce/'; | |
if ( $name ) { | |
$path = $template_directory . "{$slug}-{$name}.php"; | |
} else { | |
$path = $template_directory . "{$slug}.php"; | |
} | |
return file_exists( $path ) ? $path : $template; | |
} | |
/** | |
* Template File | |
* | |
* @param string $template Default template file path. | |
* @param string $template_name Template file name. | |
* @param string $template_path Template file directory file path. | |
* @return string Return the template file from plugin. | |
*/ | |
function override_woocommerce_template( $template, $template_name, $template_path ) { | |
// UNCOMMENT FOR @DEBUGGING | |
// echo '<pre>'; | |
// echo 'template: ' . $template . '<br/>'; | |
// echo 'template_name: ' . $template_name . '<br/>'; | |
// echo 'template_path: ' . $template_path . '<br/>'; | |
// echo '</pre>'; | |
// Template directory. | |
// E.g. /wp-content/plugins/my-plugin/woocommerce/ | |
$template_directory = untrailingslashit( plugin_dir_path( __FILE__ ) ) . 'woocommerce/'; | |
$path = $template_directory . $template_name; | |
return file_exists( $path ) ? $path : $template; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment