How to Override WooCommerce Templates in a WordPress Plugin

WooCommerce logo

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, like so:

Override WooCommerce Templates: Woocommerce directory in a plugin

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

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;
  }
}

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
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x