WooCommerce:将自定义模板添加到客户帐户页面 [英] WooCommerce: Adding custom template to customer account pages

查看:290
本文介绍了WooCommerce:将自定义模板添加到客户帐户页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将自定义页面添加到客户的帐户"部分,这将允许用户编辑其订单.目前,我已经能够设置URL的端点并进行提取,但是我需要让WooCommerce来启动页面布局并能够设置模板位置.

I am attempting to add a custom page to the customers 'account' section, which will allow the user to edit their order. At present I have been able to set an end point for the url and pick it up, but I need to get WooCommerce to initiate the page layout and be able to set the template location.

正在调用的URL:

/my-account/edit-order/55/

该文件位于 functions.php 文件中,其中设置了终点并覆盖了模板:

This is in the functions.php file, with the end point set and template override:

// 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 is a working solution for WooCommerce 2.6+ to extend and manipulate the tabbed "My Account" page endpoints (See this reference at the end of this answer), so here it is what you can do to achieve this:

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


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


Then you will need, to Insert the new Edit order endpoint into the My Account menu:

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


重要:您将需要刷新重写规则 (两种方式):

  • 转到永久链接"选项页面,然后重新保存永久链接(感谢 helgatheviking )
  • 您还可以禁用/启用主题.
  • Go to the Permalinks options page and re-save the permalinks (thanks to helgatheviking)
  • You can also disable/enable your theme.

参考文献:

  • Tabbed My Account page (WC 2.6+): Creating new endpoints

WooCommerce:在我的帐户页面

如何在woocommerce中添加新端点(旧且不完整)

这篇关于WooCommerce:将自定义模板添加到客户帐户页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆