woocommerce_locate_template从插件而不是主题过滤

时间:2016-02-08 16:39:20

标签: 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);

调用此函数:

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; 
}

导致这些日志行:

[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/

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

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

0 个答案:

没有答案