WooCommerce:将自定义模板添加到客户帐户页面

WooCommerce: Adding custom template to customer account pages

我正在尝试向客户 'account' 部分添加一个自定义页面,这将允许用户编辑他们的订单。目前我已经能够为 url 设置一个终点并将其拾取,但我需要让 WooCommerce 启动页面布局并能够设置模板位置。

被呼叫的URL:

/my-account/edit-order/55/

这在 functions.php 文件中,终点设置和模板覆盖:

// Working
add_action( 'init', 'add_endpoint' );
function add_endpoint(){
     add_rewrite_endpoint( 'edit-order', EP_ALL );
}

// need something here to check for end point and run page as woocommerce

// Not been able to test
add_filter( 'wc_get_template', 'custom_endpoint', 10, 5 );
function custom_endpoint($located, $template_name, $args, $template_path, $default_path){

    if( $template_name == 'myaccount/my-account.php' ){
        global $wp_query;
        if(isset($wp_query->query['edit-order'])){
            $located = get_template_directory() . '/woocommerce/myaccount/edit-order.php';
        }
    }

    return $located;
}

感谢您的帮助。

这是 WooCommerce 2.6+ 的工作解决方案,用于扩展和操作 选项卡式“我的帐户”页面端点(参见 this reference 在这个答案的末尾),所以这是你可以做的来实现这个:

add_action( 'init', 'custom_new_wc_endpoint' );
function custom_new_wc_endpoint() {
    add_rewrite_endpoint( 'edit-order', EP_ROOT | EP_PAGES );
}

add_filter( 'query_vars', 'custom_query_vars', 0 );
function custom_query_vars( $vars ) {
    $vars[] = 'edit-order';
    return $vars;
}

add_action( 'after_switch_theme', 'custom_flush_rewrite_rules' );    
function custom_flush_rewrite_rules() {
    flush_rewrite_rules();
}

// The custom template location
add_action( 'woocommerce_account_edit-order_endpoint', 'custom_endpoint_content' );
function custom_endpoint_content() {
    include 'woocommerce/myaccount/edit-order.php'; 
}

然后您需要将新的编辑订单端点插入我的帐户菜单:

add_filter( 'woocommerce_account_menu_items', 'custom_my_account_menu_items' );
function custom_my_account_menu_items( $items ) {
    // Remove the orders menu item.
    $orders_item = $items['orders']; // first we keep it in a variable
    unset( $items['orders'] ); // we unset it then

    // Insert your custom endpoint.
    $items['edit-order'] = __( 'Edit Order', 'woocommerce' );

    // Insert back the logout item.
    $items['orders'] = $orders_item; // we set it back

    return $items;
}

Important: You will need to flush the rewrite rules (2 ways):

  • Go to the Permalinks options page and re-save the permalinks (thanks to helgatheviking)
  • You can also disable/enable your theme.

参考文献: