【问题标题】:Woocommerce - overriding the template through a pluginWoocommerce - 通过插件覆盖模板
【发布时间】:2015-11-16 00:27:46
【问题描述】:

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

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

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

非常感谢。

【问题讨论】:

标签: php wordpress woocommerce


【解决方案1】:
  • 使用过滤器 wc_get_template_part 我们可以覆盖默认的 WooCommerce 模板部分。
  • 使用过滤器 woocommerce_locate_template 我们可以覆盖默认的 WooCommerce 模板。

试试下面的示例代码 sn-p。

<?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 3.x 模板文件的最佳方式。
  • 似乎不再起作用了?现在正在加载默认模板
  • @LatheeshVMVilla 文件路径需要添加模板。应该是以下内容:$template_directory = untrailingslashit( plugin_dir_path( __FILE__ ) ) . '/woocommerce/templates/'; 这对我有用
【解决方案2】:

几个月前,我也有同样的要求。所以在网上搜索了更多,找到了对我有帮助的有用代码(根据我的要求进行了更多的定制)。

有关详细代码,请查看thisthis 链接。该方法可能与您当前使用的不同,但它会导致在插件中覆盖 woocommerce 模板

【讨论】:

  • 感谢您的回答。这对你完全有用吗?我认为第一个和第二个链接具有相同的代码。我使用了第一个链接中的代码(只是把它变成了一个类),但它并不完全适合我。我根本无法通过它编辑商店页面,我可以更改一些文件,但不是全部。你有什么问题吗?
  • 为了更具体地说明我的需要:除了需要自定义文件之外,我必须自定义的主要文件是商店页面,因为我必须更改其整个布局。
  • 我测试了大约 10 个解决方案/代码示例(包括您在此处链接的那些),没有一个有效,只有上面的一个,由 @maheshwaghmare 提交。
【解决方案3】:

您应该尝试在// Use default template 代码之前添加此代码:

if( $template_name == '{template part name}') {
     $template = $plugin_path . $template_name;
}

在我的例子中,{template part name} 是 global/quantity-input.php

您可以通过临时将此行添加到您的代码中来找出您的确切模板部分名称:

print_r($template_name);

我知道在这里回答有点晚了,但也许对其他人有用。请记住,woocommerce_locate_template 已被贬低。所以那里可能有更多“最新”的解决方案。

【讨论】:

    猜你喜欢
    • 2013-02-26
    • 2018-06-03
    • 2013-02-08
    • 2020-08-03
    • 2017-01-26
    • 2021-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多