woocommerce_locate_template 从插件而不是主题过滤

Dav*_*ave 5 php wordpress plugins woocommerce

我发现这篇关于使用插件而不是主题来覆盖 Woocommerce 模板的文章: https://www.skyverge.com/blog/override-woocommerce-template-file-within-a-plugin/

它似乎几乎可以工作,但它不适用于 woocommerce/templates 目录根目录中的任何模板(例如 single-product.php 或 content-single-product.php)。

为了说明这一点,我有这个过滤器:

add_filter('woocommerce_locate_template', array($this, 'woocommerce_locate_template'), 10, 3);
Run Code Online (Sandbox Code Playgroud)

调用这个函数:

public function woocommerce_locate_template($template, $template_name, $template_path) { 
global $woocommerce; 
al_log('Looking for ' . $template . ', ' . $template_name . ', ' . $template_path); 
$_template = $template; 
if (! $template_path) $template_path = $woocommerce->template_url; 
$plugin_path = $this->my_plugin_path() .'/woocommerce/'; 
//look within passed path within the theme. This is the 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; 
} 
return $template; 
}
Run Code Online (Sandbox Code Playgroud)

其结果是这些日志行:

[davetbo@dev wp-content]$ [05-Feb-2016 17:10:46 UTC] Looking for /var/www/html/wp-content/plugins/woocommerce/templates/global/breadcrumb.php, global/breadcrumb.php, woocommerce/ 
[05-Feb-2016 17:10:47 UTC] Looking for /var/www/html/wp-content/plugins/woocommerce/templates/single-product/sale-flash.php, single-product/sale-flash.php, woocommerce/ 
[05-Feb-2016 17:10:47 UTC] Looking for /var/www/html/wp-content/plugins/woocommerce/templates/single-product/product-image.php, single-product/product-image.php, woocommerce/ 
[05-Feb-2016 17:10:47 UTC] Looking for /var/www/html/wp-content/plugins/woocommerce/templates/single-product/product-thumbnails.php, single-product/product-thumbnails.php, woocommerce/ 
[05-Feb-2016 17:10:47 UTC] Looking for /var/www/html/wp-content/plugins/woocommerce/templates/single-product/title.php, single-product/title.php, woocommerce/ 
[05-Feb-2016 17:10:47 UTC] Looking for /var/www/html/wp-content/plugins/woocommerce/templates/single-product/rating.php, single-product/rating.php, woocommerce/ 
[05-Feb-2016 17:10:47 UTC] Looking for /var/www/html/wp-content/plugins/woocommerce/templates/single-product/price.php, single-product/price.php, woocommerce/ 
[05-Feb-2016 17:10:47 UTC] Looking for /var/www/html/wp-content/plugins/woocommerce/templates/single-product/short-description.php, single-product/short-description.php, woocommerce/ 
[05-Feb-2016 17:10:47 UTC] Looking for /var/www/html/wp-content/plugins/woocommerce/templates/single-product/meta.php, single-product/meta.php, woocommerce/ 
[05-Feb-2016 17:10:47 UTC] Looking for /var/www/html/wp-content/plugins/woocommerce/templates/single-product/share.php, single-product/share.php, woocommerce/ 
[05-Feb-2016 17:10:47 UTC] Looking for /var/www/html/wp-content/plugins/woocommerce/templates/single-product/tabs/tabs.php, single-product/tabs/tabs.php, woocommerce/ 
[05-Feb-2016 17:10:47 UTC] Looking for /var/www/html/wp-content/plugins/woocommerce/templates/single-product/tabs/description.php, single-product/tabs/description.php, woocommerce/ 
[05-Feb-2016 17:10:47 UTC] Looking for /var/www/html/wp-content/plugins/woocommerce/templates/single-product/tabs/additional-information.php, single-product/tabs/additional-information.php, woocommerce/ 
[05-Feb-2016 17:10:47 UTC] Looking for /var/www/html/wp-content/plugins/woocommerce/templates/single-product/product-attributes.php, single-product/product-attributes.php, woocommerce/ 
[05-Feb-2016 17:10:47 UTC] Looking for /var/www/html/wp-content/plugins/woocommerce/templates/single-product/up-sells.php, single-product/up-sells.php, woocommerce/ 
[05-Feb-2016 17:10:47 UTC] Looking for /var/www/html/wp-content/plugins/woocommerce/templates/single-product/related.php, single-product/related.php, woocommerce/ 
[05-Feb-2016 17:10:47 UTC] Looking for /var/www/html/wp-content/plugins/woocommerce/templates/global/sidebar.php, global/sidebar.php, woocommerce/
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,它永远不会在 woocommerce/templates 目录的根目录中查找任何内容。关于为什么该过滤器不适用于 single-product.php 或 content-single-product.php 有什么想法吗?

目前,我的插件文件夹的根目录中有一个 woocommerce 文件夹,其中有一个 single-product.php 文件,顶部附近有一个 die 语句,所以我应该立即查看它是否正常工作。我确实将相同的 single-product.php 文件放入主题中的 woocommerce 文件夹中,并且它可以在那里工作,但不能通过插件进行。我应该使用不同的钩子吗?请指教。