Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/246.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Woocommerce-通过插件覆盖模板_Php_Wordpress_Woocommerce - Fatal编程技术网

Php Woocommerce-通过插件覆盖模板

Php Woocommerce-通过插件覆盖模板,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,我有一个问题:有没有一种方法可以通过插件覆盖WooCommerce的默认模板,就像使用主题一样? 我有以下代码: Class WoocommerceOverride { public function woocommerce_locate_template( $template, $template_name, $template_path ) { $plugin_path = SPSL_PLUGIN_PATH; global $woocommerce;

我有一个问题:有没有一种方法可以通过插件覆盖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 );
Class-WoocommerceOverride{
公共函数\u定位\u模板($template、$template\u name、$template\u path){
$plugin\u path=SPSL\u plugin\u path;
全球商业;
$\模板=$模板;
如果(!$template\u path)$template\u path=$woocommerce->template\u url;
$plugin_path.='/woocommerce/';
//查看主题内的传递路径-这是优先级
$template=locate\u模板(
排列(
$template\u路径。$template\u名称,
$template\u名称
)
);
//修改:从该插件获取模板(如果存在)
如果(!$template&&file_存在($plugin_path.$template_name))
$template=$plugin\u path.$template\u name;
//使用默认模板
如果(!$template)
$template=$\u模板;
//echo$模板。“
”; //归还我们找到的东西 返回$template; } } 添加过滤器('woocommerce'u locate'u template',数组('WoocommerceOverride','woocommerce'u locate'u template'),10,3);
这段代码的问题是它只能部分工作。它在某些部分起作用,在其他部分则不起作用。例如,我根本无法自定义archive-product.php。无论我在那里写什么,无论是代码还是纯文本,我都不会得到任何结果。 我将完全相同的模板文件从我的插件文件夹复制到我的主题文件夹中,它可以正常工作。然而,因为我需要这个插件,所以我不能走主题路线


非常感谢。

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

有关详细的代码以及解释检查和链接。该方法可能与您当前使用的方法不同,但它会覆盖插件中的woocommerce模板
  • 使用过滤器
    wc\u get\u template\u part
    我们可以覆盖默认模板部分
  • 使用过滤器
    woocommerce\u locate\u模板
    我们可以覆盖默认的woocommerce模板
  • 请尝试下面的示例代码段

    <?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;
    }
    

    您应该尝试在
    //使用默认模板之前添加此代码
    代码:

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

    通过将此行临时添加到代码中,可以找到准确的模板零件名称:


    我知道现在回答这个问题有点晚了,但也许它对其他人有用。请记住,
    woocommerce\u locate\u模板
    已被去除润滑脂。因此,可能还有更多“最新”的解决方案。

    谢谢您的回答。这对你完全有效吗?我认为第一个和第二个链接有相同的代码。我使用了第一个链接中的代码(只是把它做成了一个类),但它并不完全适合我。我根本无法通过它编辑商店页面,我可以更改一些文件,但不是全部。你有什么问题吗?更具体地说,我需要的是:除了需要自定义文件之外,我必须自定义的主要文件是商店页面,因为我必须更改它的整个布局。我测试了大约10个解决方案/代码示例(包括你在这里链接的示例),它们都不起作用,只有上面的一个,提交人@maheshwaghmare.Works非常感谢。看在谷歌的份上,我要说的是,这是从插件中替换woocommerce 3.x模板文件的最好方法。似乎不再有效了?现在,默认的模板是loaded@LatheeshVMVilla文件路径需要添加模板。应该是以下内容:
    $template\u directory=untrailingslashit(plugin\u dir\u path(\uu文件\uu))。'/woocommerce/templates/'
    在经过大量研究后对我有效:
    woocommerce\u locate\u模板
    不加载
    单个产品.php
    模板。
    if( $template_name == '{template part name}') {
         $template = $plugin_path . $template_name;
    }
    
    print_r($template_name);