wc_get_template returning null


wc_get_template returning null

我似乎无法让wc_get_template()返回我的页面模板。我确信将其存储在子主题文件夹中的路径是正确的,但当我运行它时,我会得到NULL。你看到这里有什么不对吗?

public function endpoint_content() { 
    // The template name. 
    $template_name = 'Students Details'; 
    // (default: array()) 
    $args = array(); 
    // The template path. 
    $template_path =  get_stylesheet_directory().'/woocommerce/myaccount/add_students.php';
    // NOTICE! Understand what this does before running. 
    $res = wc_get_template($template_name, $args, $template_path, $template_path);
    var_dump($res); 
}

您向wc_get_template()传递了错误的参数。

  1. $template_name是全名,所以myaccount/student-details.php
  2. $template_path是一个空字符串。通常,WC会在主题的woocommerce文件夹中查找
  3. $default_path这是指向插件模板的路径。在这种情况下,将templates文件夹添加到我在您的原始问题中为您创建的插件的根文件夹中

这是更新后的功能:

/**
 * Endpoint HTML content.
 */
public function endpoint_content() {    
    // The template name. 
    $template_name = 'myaccount/student-details.php'; 
    // default args
    $args = array(); 
    // default template
    $template_path = ''; // use default which is usually "woocommerce"
    // default path (look in plugin file!)
    $default_path = untrailingslashit( plugin_dir_path(__FILE__) ) . '/templates/';
    // call the template
    wc_get_template($template_name, $args, $template_path, $default_path);
}

这是一个示例模板wc-your-custom-endpoint-plugin/templates/student-details.php:

<?php
/**
 * Template
 * 
 * @author      Kathy Darling
 * @package     WC_Custom_Endpoint/Templates
 * @version     0.1.0
 */
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
?>
<?php do_action( 'wc_custom_endpoint_before_student_details' ); ?>
<p><?php _e( 'Hello World!', 'wc-custom-endpoint' ); ?><p>
<?php do_action( 'wc_custom_endpoint_after_student_details' ); ?>