【发布时间】:2019-10-15 16:29:08
【问题描述】:
我一直在尝试从我自己的自定义插件中对 WooCommerce 应用过滤器,但无论我做什么,过滤器都不会触发。
网上常见的解决方案不起作用。我试过了:
- Overwrite wordpress theme page by plugin
- Custom templates in Woocommerce 3
- https://www.damiencarbery.com/2017/01/override-woocommerce-template-files-in-a-plugin/
- https://javorszky.co.uk/2017/09/07/where-do-you-redeclare-a-template/
我还尝试将过滤器附加到各种操作点(init、plugins_loaded)和各种优先级(10、20、999)。
为了测试,我创建了一个简单的单页插件。当过滤器触发时,它应该写入error_log。该文件位于/wp-content/plugins/plugintest.php
<?php
/*
* Plugin Name: Plugin TEST
* Description: A simple plugin to test
* Version: 1.0
**/
function my_test_get_template( $template, $template_name, $args ) {
error_log('working ...... FILTER .... ');
return $template;
}
error_log('working ... ');
add_filter( 'woocommerce_locate_template', 'my_test_get_template', 20, 3);
但是过滤器内部的error_log永远不会被写入,输出只是
[15-Oct-2019 16:20:52 UTC] working ...
我不明白为什么代码不起作用。我知道 WooCommerce 是活跃的,并且可以像我编写的其他代码(扩展 WC_EMAIL 类)一样工作。而且我可以毫无问题地过滤“woocommerce_email_classes”,这也有效。但是模板路径不会触发。有什么明显的我做错了吗?
【问题讨论】:
标签: php wordpress woocommerce