首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Woocommerce - woocommerce_locate_template的替代方案

Woocommerce - woocommerce_locate_template的替代方案
EN

Stack Overflow用户
提问于 2015-09-07 22:01:00
回答 3查看 4.5K关注 0票数 7

我正在开发一个基于woocommerce的插件,作为其中的一部分,我必须覆盖woocommerce的默认模板文件位置。我的意思是,我希望有一个定制的woocommerce模板加载从我的插件。

为此,我在woocommerce中阅读了基于这个woocommerce_locate_template的文章,但是我注意到,与这个链接一样,这个函数也被废弃了。现在我想知道有什么可以替代的功能。

我的全部意图是更改默认的woocommerce模板加载位置到我的插件文件夹。对解决这件事有帮助吗?提前谢谢。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-09-07 22:26:56

woocommerce_locate_template函数被废弃为wc_locate_template:您可以读取代码这里

但是,如果您正在寻找woocommerce_locate_template筛选器,那么它仍然是,并接受三个参数:

  1. $template,即wp核心函数locate_template的结果。
  2. 仅为文件名的$template_name
  3. $template_path是用于模板的woocommerce路径

因此,您可以检查$template_name是否是您要拦截的内容,如果为真,则更改路径,如下所示

代码语言:javascript
复制
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);

我还没有测试它,很抱歉有任何可能的语法错误:)

希望能帮上忙!

-更新1:我忘了一个分号:P --

-更新2:我犯了一个错误!-

票数 12
EN

Stack Overflow用户

发布于 2015-12-29 15:17:15

我必须修改上面的代码,以使其与我所需的模板文件正确匹配,在我的例子中,这是"variable.php“。

$template_name需要成为整个woocommerce的根路径,如下所示:

见以下经修订的守则:

代码语言:javascript
复制
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);
票数 5
EN

Stack Overflow用户

发布于 2021-11-17 22:54:51

如果有人在2021年与此做斗争,那么值得一提的是, filter 'woocommerce_locate_template' 并没有过滤woocommerce文件夹中的所有模板。相反,您需要过滤其他两个函数:

代码语言:javascript
复制
add_filter('wc_get_template', 'entex_wc_get_template', 20, 5);
add_filter('wc_get_template_part', 'entex_wc_get_template_part', 20, 3);

例如,根woocommerce模板content-single-product.php必须使用wc_get_template_part进行过滤。

这是为我们的插件工作:

代码语言:javascript
复制
function template_base(){
    return untrailingslashit(plugin_dir_path( __FILE__ )) .'/templates/';
}

function entex_wc_get_template($template, $template_name, $args, $template_path, $default_path){

    /* custom theme templates has priority */
    if(strpos($template, '/themes/') !== FALSE) return $template;

    static $cache = array();
    if(isset($cache[$template_name])) return $cache[$template_name];
    
    $plugin_template = wc_locate_template($template_name, WC()->template_path(), $this->template_base());
    if($plugin_template && file_exists($plugin_template)){
        $template = $plugin_template;
        $cache[$template_name] = $template;
    }
    return $template;
}

function entex_wc_get_template_part($template, $slug, $name){
    
    /* custom theme templates has priority */
    if(strpos($template, '/themes/') !== FALSE) return $template;
    
    $template_name = '';
    if($name){
        $template_name = "{$slug}-{$name}.php";
    } else if($slug){
        $template_name = "{$slug}.php";
    }
    if(!$template_name) return $template;
    
    static $cache = array();
    if(isset($cache[$template_name])) return $cache[$template_name];
    
    $plugin_template = template_base().$template_name;
    if($plugin_template && file_exists($plugin_template)){
        $template = $plugin_template;
        $cache[$template_name] = $template;
    }
    return $template;
} 

这是从PHP类中删除并粘贴到这里的,因此希望代码不会中断。

我们建议注册模板以获得更干净的性能,如果您只使用几个模板的话,并在函数的早期添加如下内容:

代码语言:javascript
复制
if(!in_array($template_name, array(
    'archive-product.php',
    'content-product.php',
    'content-product-cat.php',
    'content-single-product.php',
    'content-widget-product.php',
    'checkout/form-checkout.php',
    'checkout/thankyou.php',
    'loop/loop-start.php',
    'loop/loop-end.php'
))) return $template;
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32446556

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档