I have a question: is there a way to override WooCommerce’s default template through a plugin the same way you’d do it with a theme?
I have this code:
Class WoocommerceOverride {
public function woocommerce_locate_template( $template, $template_name, $template_path ) {
$plugin_path = SPSL_PLUGIN_PATH;
global $woocommerce;
$_template = $template;
if ( ! $template_path ) $template_path = $woocommerce->template_url;
$plugin_path .= '/woocommerce/';
// Look within passed path within the theme - this is priority
$template = locate_template(
array(
$template_path . $template_name,
$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;
//echo $template."<br>";
// Return what we found
return $template;
}
}
add_filter( 'woocommerce_locate_template', array('WoocommerceOverride', 'woocommerce_locate_template'), 10, 3 );
The problem with this code is that it works only partially. On some parts it works, on other parts it does not. For example, I can’t customize archive-product.php at all. Whatever I write in there, whether code or plain text, I just don’t get any results.
I copied the exact same template files from my plugin folder into my theme folder and it works. However, as I need this as a plugin, I can’t go the theme route.
Many thanks.
wc_get_template_part
we can override default WooCommerce template part’s.woocommerce_locate_template
we can override default WooCommerce template’s.Try below example code snippet.
Few months ago the i had the same requirements. So searched a bit more on the net and found useful code which helped me(with a little more customization as per my requirements).
For a detailed code with explanation check this and this link. The approach might be different than what you currently using but it results in overriding woocommerce templates in plugin
You should try adding this code before your
// Use default template
code:In my case {template part name} was
global/quantity-input.php
You can find out your exact template part names by temporary adding this line to your code:
I know it’s a bit late for answer here but maybe it will be useful for someone else. And keep in mind
woocommerce_locate_template
is depricated. So there is probably more ‘up to date’ solution somewhere out there.