From The Blog

Wordpress: WooCommerce plugin: How to Override WooCommerce Template File Within a Plugin

30 December 2013, in WordPress

Override WooCommerce Template File Within a Plugin

The goal of this article is to describe one approach to overriding WooCommerce core template files within a custom plugin, such that they can still beoverridden by a theme in the typical way. Note that this is similar to but solves a slightly different problem than what we did to create a custom overrideable woocommerce template file in your custom plugin. Overriding a core template in your plugin is something you might want to do if you're creating a WooCommerce plugin that needs to alter a core template file, and you don't want to have to supply a separate template file for dropping into the theme, and yet you also want the flexibility of allowing the template to be overridden in the theme. Thankfully WooCommerce's flexible template loading system offers a lot of control over where template files are loaded from, and allows us to do just this. The normal WooCommerce template loader searches the following locations in order, until a match is found:

  1. your theme / template path / template name
  2. your theme / template name
  3. default path / template name

We're going to alter this slightly by injecting a search for the template within our own custom plugin (step 3 below), before finally defaulting to the WooCommerce core templates directory:

  1. your theme / template path / template name
  2. your theme / template name
  3. your plugin / woocommerce / template name
  4. default path / template name

This can be done by adding the following function and filter, which basically duplicates and modifies the behavior of the woocommerce_locate_template()function found within woocommerce-core-functions.php:

functionmyplugin_plugin_path() {
  // gets the absolute path to this plugin directory
  returnuntrailingslashit( plugin_dir_path( __FILE__) );
add_filter( 'woocommerce_locate_template', 'myplugin_woocommerce_locate_template', 10, 3 );
functionmyplugin_woocommerce_locate_template( $template, $template_name, $template_path) {
  $_template= $template;
  if( ! $template_path) $template_path= $woocommerce->template_url;
  $plugin_path= myplugin_plugin_path() . '/woocommerce/';
  // Look within passed path within the theme - this is priority
  $template= locate_template(
      $template_path. $template_name,
  // Modification: Get the template from this plugin, if it exists
  if( ! $template&& file_exists( $plugin_path. $template_name) )
    $template= $plugin_path. $template_name;
  // Use default template
  if( ! $template)
    $template= $_template;
  // Return what we found
With that active you can override core template files by placing them in myplugin/woocommerce/. For instance, to override loop/add-to-cart.php, copy that file to your plugin in the following location:myplugin/woocommerce/loop/add-to-cart.phpand make your modifications. The theme will still be able to override it, and all other template files will be loaded from WooCommerce or the default path, as normal.

Post a Comment