How to Override WooCommerce Templates in a WordPress Plugin

We know how to override WooCommerce templates from themes, but how about doing so from a plugin?

It’s fairly straightforward by using the WooCommerce woocommerce_locate_template filter.

First, create a directory in your plugin named woocommerce.

Next, create a constant in your main plugin file pointing to the folder you just created (this makes it easier to reference in the future):

defined( 'BSD_PLUGIN_BOILERPLATE_WOO_TEMPLATE_DIR' ) or define( 'BSD_PLUGIN_BOILERPLATE_WOO_TEMPLATE_DIR', untrailingslashit( plugin_dir_path( __FILE__ ) ) . '/woocommerce/' );Code language: PHP (php)

Now, add your hook and function:

add_filter( 'woocommerce_locate_template', 'bsd_woocommerce_locate_template', 10, 3 );
if ( ! function_exists( 'bsd_woocommerce_locate_template' ) ) {
  public function bsd_woocommerce_locate_template( $template, $template_name, $template_path ) {
    $path = BSD_PLUGIN_BOILERPLATE_WOO_TEMPLATE_DIR . $template_name;
    return file_exists( $path ) ? $path : $template;
}Code language: PHP (php)

That’s all there is to it! Now WooCommerce templates added to your woocommerce folder will be loaded and evaluated by WooCommerce.

0 0 votes
Article Rating
Notify of
Inline Feedbacks
View all comments
Would love your thoughts, please comment.x