在Woocommerce我的帐户页面中包含自定义模板文件问题

时间:2018-03-21 02:53:41

标签: php wordpress templates woocommerce account

我正在尝试添加一个包含位于我的活动子主题中的模板文件:

childtheme/woocommerce/myaccount/order-a-kit.php

该函数也使用echo "Hello World"成功显示,但不包括php模板文件。

我试过那些:

include($_SERVER['DOCUMENT_ROOT']."twentyseventeen-child/woocommerce/myaccount/order-a-kit.php");

include($get_stylesheet_directory_uri()."twentyseventeen-child/woocommerce/myaccount/order-a-kit.php");

include 'twentyseventeen-child/woocommerce/myaccount/order-a-kit.php';

order-a-kit.php的内容非常简单,我只想尝试包含该文件:

<?php
?>

<div>
    <p>
        Look at me
    </p>
</div>

这是我的function.php部分,除了包含底部的功能外,一切都正常:

add_filter( 'woocommerce_account_menu_items', 'add_my_menu_items', 99, 1 );

function add_my_menu_items( $items ) {
    $my_items = array(
    //  endpoint   => label
        'order-a-kit' => __( 'Order A Kit', 'woocommerce'),
        'orders' => __( 'Order History', 'my_plugin' ),
    );

    $my_items = array_slice( $items, 0, 1, true ) +
        $my_items +
        array_slice( $items, 1, count( $items ), true );

    return $my_items;
}


//adding custom endpoint 

function my_custom_endpoints() {
    add_rewrite_endpoint( 'order-a-kit', EP_ROOT | EP_PAGES );
}

add_action( 'init', 'my_custom_endpoints' );

function my_custom_query_vars( $vars ) {
    $vars[] = 'order-a-kit';

    return $vars;
}

add_filter( 'query_vars', 'my_custom_query_vars', 0 );

function my_custom_flush_rewrite_rules() {
    flush_rewrite_rules();
}

add_action( 'wp_loaded', 'my_custom_flush_rewrite_rules' );

//including custom endpoint

function my_custom_endpoint_content() {
    include 'twentyseventeen-child/woocommerce/myaccount/order-a-kit.php';
    echo '<p>Hello World!</p>';
}

add_action( 'woocommerce_account_order-a-kit_endpoint', 'my_custom_endpoint_content' );


?>

非常感谢任何帮助。

3 个答案:

答案 0 :(得分:1)

您可以将此代码添加到主题的function.php中:

class My_Custom_My_Account_Endpoint {
/**
 * Custom endpoint name.
 *
 * @var string
 */
public static $endpoint = 'Your Desired Link';
/**
 * Plugin actions.
 */
public function __construct() {
    // Actions used to insert a new endpoint in the WordPress.
    add_action( 'init', array( $this, 'add_endpoints' ) );
    add_filter( 'query_vars', array( $this, 'add_query_vars' ), 0 );
    // Change the My Accout page title.
    add_filter( 'the_title', array( $this, 'endpoint_title' ) );
    // Insering your new tab/page into the My Account page.
    add_filter( 'woocommerce_account_menu_items', array( $this, 'new_menu_items' ) );
    add_action( 'woocommerce_account_' . self::$endpoint .  '_endpoint', array( $this, 'endpoint_content' ) );
}
/**
 * Register new endpoint to use inside My Account page.
 *
 * @see https://developer.wordpress.org/reference/functions/add_rewrite_endpoint/
 */
public function add_endpoints() {
    add_rewrite_endpoint( self::$endpoint, EP_ROOT | EP_PAGES );
}
/**
 * Add new query var.
 *
 * @param array $vars
 * @return array
 */
public function add_query_vars( $vars ) {
    $vars[] = self::$endpoint;
    return $vars;
}
/**
 * Set endpoint title.
 *
 * @param string $title
 * @return string
 */
public function endpoint_title( $title ) {
    global $wp_query;
    $is_endpoint = isset( $wp_query->query_vars[ self::$endpoint ] );
    if ( $is_endpoint && ! is_admin() && is_main_query() && in_the_loop() && is_account_page() ) {
        // New page title.
        $title = __( 'Your Item Name', 'woocommerce' );
        remove_filter( 'the_title', array( $this, 'endpoint_title' ) );
    }
    return $title;
}
/**
 * Insert the new endpoint into the My Account menu.
 *
 * @param array $items
 * @return array
 */
public function new_menu_items( $items ) {
    // Remove the logout menu item.
    $logout = $items['customer-logout'];
    unset( $items['customer-logout'] );
    // Insert your custom endpoint.
    $items[ self::$endpoint ] = __( 'Your Item Name', 'woocommerce' );
    // Insert back the logout item.
    $items['customer-logout'] = $logout;
    return $items;
}
/**
 * Endpoint HTML content.
 */
public function endpoint_content() {
    //example include('woocommerce/myaccount/Your-File.php');
    include('Path-To-Your-File.php');
}
/**
 * Plugin install action.
 * Flush rewrite rules to make our custom endpoint available.
 */
public static function install() {
    flush_rewrite_rules();
}
}
new My_Custom_My_Account_Endpoint();
// Flush rewrite rules on plugin activation.
register_activation_hook( __FILE__, array( 'My_Custom_My_Account_Endpoint', 'install' ) );

您只需在此源代码中简单设置“您的所需链接” x1和“您的商品名称” x2和“路径到您的File.php” x1。
如果您不知道主题的function.php在哪里:

1。登录WordPress管理界面
  2.在左侧边栏中,将鼠标悬停在外观上,然后单击主题编辑器
  3.在右侧栏中,单击functions.php

答案 1 :(得分:0)

试试这个

require_once plugin_dir_path( dirname( __FILE__ ) ) . 'woocommerce/myaccount/order-a-kit.php';

答案 2 :(得分:0)

由于“ woocommerce ”文件夹位于主题内,您只需使用function.php文件:

include 'woocommerce/myaccount/order-a-kit.php';

请参阅此相关答案:WooCommerce: Adding custom template to customer account pages