You are missing an important part.
Within the template_include hook, you need to do a check, if the other template file, which you would like to overwrite, exists. Then add a logic according to your plugin.
Also, you are messing up the path, by declaring it first, then using plugin_dir_path again in the output, so that is also a bit wrong. You need to pass in the variable for the original template, then – catch it – before delivering it to the output.
I have done this in the past via the “template_include” hook. A filter may also work, though I cannot confirm that.
This is my solution to this, which works in different situations for overwriting a theme file with a plugin file. I haven’t tested this, but it should work.
<?php
function wpse_template_logic( $original_template ) {
// Let's build a directory variable for the paths
$dir = plugins_url('/your-plugin-folder-name');
// Check the theme template for wc loop-start.php
// Make sure you establish the correct paths by "get_template_directory_uri()" and "plugins_url()" function
// Also, be sure to "only" trigger the action, when a certain condition is met
// You don't want to trigger this every time, just when the template is requested
if (is_page('whatever_wc_page') || $whatever_condition_you_want_to_check) {
// Now check to see, if the file in the folder XY exists, then overwrite it
// The outer file_exists check will return true or false, if or if not the template exists in your theme folder, if it does, then move to the plugin folder to get the template
// This logic is up to you more or less, according to your strategy
// use the $dir variable to get to the your directory and the template
if(file_exists(get_template_directory_uri().'/woocommerce/loop/loop-start.php')){
return $dir . '/custom-wc-template-folder-in-your-plugin/loop-start.php';
}
return $original_template;
}
// First argument is the hook, then the function name, which you want to execute, then the priority, then the amount of arguments
add_action( 'template_include', 'wpse_template_logic', 10, 1 );
For problems like this in development, I’d highly recomment installing the plugin “What the file”. It will show you the template name, which is currently used. Another good plugin to debug things like this is the “Query Monitor”. It also has functions to show you the path of things (Themes and plugins) also – very, very useful. Maybe that helps.
I hope I could help a bit. In case it worked i’d be happy if you accept the answer as correct, thank you.