You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WooCommerce模板系统问题:wc_get_template加载自定义模板失败

It looks like the missing piece here is telling WooCommerce to look for your template files in your plugin's directory—by default, wc_get_template() only checks the active theme's WooCommerce folder and WooCommerce's core templates. Here's how to fix this step by step:

1. Update Your Plugin's Main File (pluginname.php)

You need to add a filter to hook into WooCommerce's template lookup system, so it checks your plugin's templates folder first. Also, make sure your template-loading function is hooked to an appropriate action (so it outputs in the right place):

<?php
/**
 * Plugin Name: Your Custom WooCommerce Plugin
 */

// Check if WooCommerce is active
if (in_array('woocommerce/woocommerce.php', apply_filters('active_plugins', get_option('active_plugins')))) {

    // Filter to add our plugin's templates to WooCommerce's search path
    function my_plugin_add_woocommerce_templates($template, $template_name, $template_path) {
        // Get the full path to our plugin's templates directory
        $plugin_template_dir = plugin_dir_path(__FILE__) . 'templates/';
        $plugin_template_file = $plugin_template_dir . $template_name;

        // If our template exists, use it instead of the default
        if (file_exists($plugin_template_file)) {
            $template = $plugin_template_file;
        }

        return $template;
    }
    add_filter('woocommerce_locate_template', 'my_plugin_add_woocommerce_templates', 10, 3);

    // Function to load and output our custom template
    function my_plugin_load_test_template() {
        // Adjust this condition to target where you want the template to appear
        // Example: Load on a specific page, product page, etc.
        if (is_page('test-page')) {
            wc_get_template('testing-template.php');
        }
    }
    // Hook to a WooCommerce action to output the template (adjust hook as needed)
    add_action('woocommerce_before_main_content', 'my_plugin_load_test_template');

}

2. Verify Your Template File

Make sure your templates/testing-template.php is correctly named and contains your content:

<h1>Output stuff</h1>

Key Troubleshooting Tips

  • Double-check file names: Ensure the template name in wc_get_template() exactly matches the file name (case-sensitive on Linux servers).
  • Confirm the template path: Use echo plugin_dir_path(__FILE__); to verify the path to your plugin directory is correct.
  • Check action hooks: If your template isn't showing up, make sure you're using the right action hook (e.g., woocommerce_after_single_product_summary for product pages, init if you need to load it earlier, but be careful with output timing).
  • Debug missing templates: Add error_log('Looking for template: ' . $plugin_template_file); inside the filter function to check if WooCommerce is finding your file.

内容的提问来源于stack exchange,提问作者simon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 07:31:56