Alex Kirwan
Alex Kirwan

Reputation: 65

wc_get_template returning null

I can't seem to get the wc_get_template() to return my page template. I am sure the path is correct to where I have it stored in my child theme folder but when I run it I get NULL. Can you see anything wrong here?

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

Upvotes: 4

Views: 4663

Answers (1)

helgatheviking
helgatheviking

Reputation: 26329

You are passing the wrong arguments to wc_get_template().

  1. $template_name is the full name so myaccount/student-details.php
  2. $template_path is an empty string. Usually, WC will look in the theme's woocommerce folder
  3. $default_path this is the path to your plugin's templates. In this case add a templates folder into the root folder of the plugin I created for you in your original question

Here's the updated function:

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

}

And here's a sample template 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' ); ?>

Upvotes: 8

Related Questions