To begin with, my problem:
- I have a
WooCommerce
error on theW3C Validator
platform. - I overwrite the file by redirecting the template path of
WooCommerce
for that specific file.
Problem:
- The
WordPress Theme
that I am currently using is also overwritting the same file as myCustom plugin
, as a result theTheme's
overwriting cancels out myplugin's
overwritting functionality.
CODE:
Here’s some of my code:
This is the function I add using the built in wordpress function add_filter() with high priority value of "1" plus the path of the file I am overwritting is… woocommerce/templates/single-product/add-to-cart/variable.php
function wc_locate_template( $template, $template_name, $template_path ) {
// Check if WooCommerce is present on the website
if ( empty( $template ) || ! is_callable( 'WC' ) || ( defined( 'WC_TEMPLATE_DEBUG_MODE' ) && WC_TEMPLATE_DEBUG_MODE ) ) {
return $template;
}
// Variables
$default_path = WC()->plugin_path() . '/templates/';
$plugin_path = untrailingslashit( plugin_dir_path( __FILE__ ) ) . '/templates/';
$template_new = str_replace( $default_path, $plugin_path, $template );
// Processing
if (
false !== strpos( $template, $default_path )
&& file_exists( $template_new )
) {
$template = $template_new;
}
// Output
return $template;
}
add_filter( 'woocommerce_locate_template', 'wc_locate_template', 1, 3 );
Questions:
How can I make sure that my plugin will overwrite the file of in such way or prority so that the theme’s overwritting won’t overlap it?
Useful links:
Thank you in advance for your time.
3
Answers
That’s an example on how to override a woocommerce template with a plugin:
Then you should be able to reach your goal by copying the template you want to override in your plugin folder.
Example:
your_plugin/template/WooCommerce/single-product/add-to-cart/variable.php
Hooks and filter are modification which runs based on priority,
the highest the priority before its being called will be the its final output
Here’s an example
Assuming I have this function
then I have all these filters with different priorities
Here is the output
if you call
youFree()
onwp_head
but if you call that
youFree()
function onwp_footer
So in your situation,
but if your having a hard at where to inject it your best option is to find it using query monitor
priority 1 means your function will be applied first, so any other function attached to the filter with a number higher than 1 will simply overwrite your function.
You need to put highest possible number to make sure your function will be the last one applied.
use
PHP_INT_MAX
to make your function the last one applied.