kri*_*a89 5 php wordpress woocommerce
我正在开发一个基于woocommerce的插件,作为我不得不覆盖woocommerce的默认模板文件位置的一部分.我的意思是我希望从我的插件中加载一个自定义的woocommerce模板.
为此,我根据本文阅读了woocommerce中的woocommerce_locate_template ,但我注意到根据此链接已弃用相同的功能.现在我想知道什么是替代功能.
我的目的是将默认的woocommerce模板加载位置更改为我的插件文件夹.有什么帮助解决这个问题?提前致谢.
Lor*_*tar 10
不推荐使用woocommerce_locate_template函数来支持wc_locate_template:您可以在此处阅读代码.
但是,如果您正在寻找过滤器,它仍然是woocommerce_locate_template并采用三个参数:
因此,您可以检查$ template_name是否是您要截取的内容,如果为true,则更改路径,如下所示
function intercept_wc_template($template, $template_name, $template_path) {
if ($template_name == 'that_template.php') {
$template = 'the/path/of/your/plugin/template.php';
}
return $template;
}
add_filter('woocommerce_locate_template', 'intercept_wc_template', 20, 3);
Run Code Online (Sandbox Code Playgroud)
我没有测试过,很抱歉任何可能的语法错误:)
希望能帮助到你!
- 更新1:我忘记了分号:P -
- 更新2:我犯了一个错误!-
我必须修改上面的代码才能让它正确匹配我需要的模板文件,在我的例子中是“variable.php”。
$template_name 需要是完整的 woocommerce 根路径,见下文:
请参阅以下修改后的代码:
function intercept_wc_template($template, $template_name, $template_path) {
if ($template_name == 'single-product/add-to-cart/variable.php') {
$template = 'wp-content/themes/theme-name/woocommerce/single-product/add-to-cart/variable.php';
}
return $template;
}
add_filter('woocommerce_locate_template', 'intercept_wc_template', 20, 3);
Run Code Online (Sandbox Code Playgroud)