Woocommerce - 通过插件覆盖模板 [英] Woocommerce - overriding the template through a plugin

查看:35
本文介绍了Woocommerce - 通过插件覆盖模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题:有没有办法像使用主题一样通过插件覆盖 WooCommerce 的默认模板?我有这个代码:

类 WoocommerceOverride {公共函数 woocommerce_locate_template( $template, $template_name, $template_path ) {$plugin_path = SPSL_PLUGIN_PATH;全球 $woocommerce;$_template = $template;如果(!$template_path)$template_path = $woocommerce->template_url;$plugin_path .='/woocommerce/';//查看主题中传递的路径 - 这是优先级$template = locate_template(大批($template_path .$template_name,$template_name));//修改:从这个插件中获取模板,如果存在if ( ! $template && file_exists( $plugin_path . $template_name ) )$template = $plugin_path .$template_name;//使用默认模板如果(!$模板)$template = $_template;//echo $template."
";//返回我们找到的返回 $template;}}add_filter( 'woocommerce_locate_template', array('WoocommerceOverride', 'woocommerce_locate_template'), 10, 3);

这段代码的问题在于它只能部分工作.在某些部分它有效,在其他部分它不起作用.例如,我根本无法自定义 archive-product.php.无论我在那里写什么,无论是代码还是纯文本,我都没有得到任何结果.我将完全相同的模板文件从我的插件文件夹复制到我的主题文件夹中,它可以工作.但是,因为我需要它作为插件,所以我不能走主题路线.

非常感谢.

解决方案

  • 使用过滤器 wc_get_template_part 我们可以覆盖默认的 WooCommerce 模板部分.
  • 使用过滤器woocommerce_locate_template,我们可以覆盖默认的 WooCommerce 模板.

试试下面的示例代码片段.

';//echo '模板:' .$模板.'<br/>';//echo 'slug: ' .$slug .'<br/>';//回显 'name: ' .$名称.'<br/>';//echo '</pre>';//模板目录.//例如/wp-content/plugins/my-plugin/woocommerce/$template_directory = untrailingslashit( plugin_dir_path( __FILE__ ) ) .'woocommerce/';如果 ( $name ) {$path = $template_directory ."{$slug}-{$name}.php";} 别的 {$path = $template_directory ."{$slug}.php";}返回 file_exists( $path ) ?$path : $template;}/*** 模板文件** @param string $template 默认模板文件路径.* @param string $template_name 模板文件名.* @param string $template_path 模板文件目录文件路径.* @return string 从插件返回模板文件.*/function override_woocommerce_template( $template, $template_name, $template_path ) {//取消@DEBUGGING 的注释//echo '

';//echo '模板:' .$模板.'<br/>';//echo 'template_name: ' .$template_name .'<br/>';//echo 'template_path: ' .$template_path .'<br/>';//echo '</pre>';//模板目录.//例如/wp-content/plugins/my-plugin/woocommerce/$template_directory = untrailingslashit( plugin_dir_path( __FILE__ ) ) .'woocommerce/';$path = $template_directory .$template_name;返回 file_exists( $path ) ?$path : $template;}

I have a question: is there a way to override WooCommerce's default template through a plugin the same way you'd do it with a theme? I have this code:

Class WoocommerceOverride {

    public function woocommerce_locate_template( $template, $template_name, $template_path ) {
        $plugin_path = SPSL_PLUGIN_PATH;
        global $woocommerce;
        $_template = $template;
        if ( ! $template_path ) $template_path = $woocommerce->template_url;
        $plugin_path .= '/woocommerce/';

  // Look within passed path within the theme - this is 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;

        //echo $template."<br>";

  // Return what we found
        return $template;
    }

}
add_filter( 'woocommerce_locate_template', array('WoocommerceOverride', 'woocommerce_locate_template'), 10, 3 );

The problem with this code is that it works only partially. On some parts it works, on other parts it does not. For example, I can't customize archive-product.php at all. Whatever I write in there, whether code or plain text, I just don't get any results. I copied the exact same template files from my plugin folder into my theme folder and it works. However, as I need this as a plugin, I can't go the theme route.

Many thanks.

解决方案

  • Using filters wc_get_template_part we can override default WooCommerce template part's.
  • Using filters woocommerce_locate_template we can override default WooCommerce template's.

Try below example code snippet.

<?php
/**
 * Override default WooCommerce templates and template parts from plugin.
 * 
 * E.g.
 * Override template 'woocommerce/loop/result-count.php' with 'my-plugin/woocommerce/loop/result-count.php'.
 * Override template part 'woocommerce/content-product.php' with 'my-plugin/woocommerce/content-product.php'.
 *
 * Note: We used folder name 'woocommerce' in plugin to override all woocommerce templates and template parts.
 * You can change it as per your requirement.
 */
// Override Template Part's.
add_filter( 'wc_get_template_part',             'override_woocommerce_template_part', 10, 3 );
// Override Template's.
add_filter( 'woocommerce_locate_template',      'override_woocommerce_template', 10, 3 );
/**
 * Template Part's
 *
 * @param  string $template Default template file path.
 * @param  string $slug     Template file slug.
 * @param  string $name     Template file name.
 * @return string           Return the template part from plugin.
 */
function override_woocommerce_template_part( $template, $slug, $name ) {
    // UNCOMMENT FOR @DEBUGGING
    // echo '<pre>';
    // echo 'template: ' . $template . '<br/>';
    // echo 'slug: ' . $slug . '<br/>';
    // echo 'name: ' . $name . '<br/>';
    // echo '</pre>';
    // Template directory.
    // E.g. /wp-content/plugins/my-plugin/woocommerce/
    $template_directory = untrailingslashit( plugin_dir_path( __FILE__ ) ) . 'woocommerce/';
    if ( $name ) {
        $path = $template_directory . "{$slug}-{$name}.php";
    } else {
        $path = $template_directory . "{$slug}.php";
    }
    return file_exists( $path ) ? $path : $template;
}
/**
 * Template File
 *
 * @param  string $template      Default template file  path.
 * @param  string $template_name Template file name.
 * @param  string $template_path Template file directory file path.
 * @return string                Return the template file from plugin.
 */
function override_woocommerce_template( $template, $template_name, $template_path ) {
    // UNCOMMENT FOR @DEBUGGING
    // echo '<pre>';
    // echo 'template: ' . $template . '<br/>';
    // echo 'template_name: ' . $template_name . '<br/>';
    // echo 'template_path: ' . $template_path . '<br/>';
    // echo '</pre>';
    // Template directory.
    // E.g. /wp-content/plugins/my-plugin/woocommerce/
    $template_directory = untrailingslashit( plugin_dir_path( __FILE__ ) ) . 'woocommerce/';
    $path = $template_directory . $template_name;
    return file_exists( $path ) ? $path : $template;
}

这篇关于Woocommerce - 通过插件覆盖模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆