从我的插件文件夹加载并自定义archive-product.php


WooCommerce Load and customize archive-product.php from my Plugin Folder

我试图从my-plugin文件夹加载模板文件,而不是使用默认的woocommerce模板。我想从my-plugin文件夹模板结构定制商店页面库,所以我编辑archive-product.php来评论和检查哪个显示空白页面或不显示空白页面,但它加载默认的woocommerce模板(archive-product.php)。我已经将模板文件移动到my-plugin/woocommerce/文件夹这是我的代码,我尝试

 function cc_woocommerce_locate_template($template, $template_name, $template_path) {
        global $woocommerce;
        global $post;
        $plugin_path = untrailingslashit(plugin_dir_path(__FILE__)) . '/woocommerce/';
    //returns my-plugin folder with base file name echo $plugin_path;
               //   echo $template_name;
            if (file_exists($plugin_path . $template_name)) {
                $template = $plugin_path . $template_name;
               // var_dump($template);
                return $template;
            }
        return $template;
    }

,我指向的钩子是

add_filter('woocommerce_locate_template', 'cc_woocommerce_locate_template', 1, 3);

我测试了提供的代码,它运行没有问题,所以在这种情况下最好重新开始。创建一个新插件并粘贴下面的代码。它包含两个过滤器:

wc_get_template_part -用于循环中使用的模板

woocommerce_locate_template -用于所有其他模板


//
// get path for templates used in loop ( like content-product.php )
add_filter( 'wc_get_template_part', function( $template, $slug, $name ) 
{ 
    // Look in plugin/woocommerce/slug-name.php or plugin/woocommerce/slug.php
    if ( $name ) {
        $path = plugin_dir_path( __FILE__ ) . WC()->template_path() . "{$slug}-{$name}.php";    
    } else {
        $path = plugin_dir_path( __FILE__ ) . WC()->template_path() . "{$slug}.php";    
    }
    return file_exists( $path ) ? $path : $template;
    
}, 10, 3 );

// get path for all other templates.
add_filter( 'woocommerce_locate_template', function( $template, $template_name, $template_path ) 
{ 
    $path = plugin_dir_path( __FILE__ ) . $template_path . $template_name;  
    return file_exists( $path ) ? $path : $template;
    
}, 10, 3 );

现在在插件主文件夹中创建子文件夹woocommerce,并复制原始文件archive-product.phpcontent-product.php。最后,在这些文件中做一些小修改(回显一些东西),看看它们是否被加载。我测试了这个例子,在以下层次结构中加载模板没有问题:

主题/template_path/template_name

主题/template_name

插件/template_path/template_name

默认/template_name

我找到了从插件加载archive-product.php或single-product.php的解决方案。我想它应该能行。

public function include_template_function( $template_path ) {
    if ( get_post_type() == 'product' ) {
        if ( is_single() ) {
            // checks if the file exists in the theme first,
            // otherwise serve the file from the plugin
            if ( $theme_file = locate_template( array ( 'single-product.php' ) ) ) {
                $template_path = $theme_file;
            } else {
                $template_path = plugin_dir_path( __FILE__ ) . 'woocommerce/single-product.php';
            }
        }
        if ( is_archive() ) {
            // checks if the file exists in the theme first,
            // otherwise serve the file from the plugin
            if ( $theme_file = locate_template( array ( 'archive-product.php' ) ) ) {
                $template_path = $theme_file;
            } else {
                $template_path = plugin_dir_path( __FILE__ ) . 'woocommerce/archive-product.php';
            }
        }
    }
    _log($template_path);
    return $template_path;
}
add_filter( 'template_include', array($this, 'include_template_function' ), 100 );