简体   繁体   English

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

[英]WooCommerce: Adding custom template to customer account pages

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. 目前我已经能够设置网址的终点并将其选中,但我需要让WooCommerce启动页面布局并能够设置模板位置。

The URL being called: 被调用的URL:

/my-account/edit-order/55/

This is in the functions.php file, with the end point set and template override: 这是在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;
}

Thanks for any help. 谢谢你的帮助。

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: 这是WooCommerce 2.6+的工作解决方案,用于扩展和操作选项卡式“我的帐户”页面端点 (请参阅本答案末尾的此参考 ),因此,您可以执行以下操作:

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

Important: You will need to flush the rewrite rules (2 ways) : 重要提示:您需要刷新重写规则 (2种方式)

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

References: 参考文献:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 WooCommerce:在我的帐户页面中为自定义模板分配端点 - WooCommerce: Assigning an endpoint to a custom template in my account pages 将一些我的帐户自定义字段添加到 Woocommerce 的管理员用户页面 - Adding some my account custom fields to admin user pages in Woocommerce WooCommerce客户备注添加多个自定义字段 - Adding multiple custom fields to WooCommerce customer note 在Woocommerce我的帐户仪表板模板中添加客户电子邮件 - Add the customer email in Woocommerce My Account Dashboard template 仅允许在我的帐户上编辑每个客户一次自定义字段 > 在 WooCommerce 中编辑帐户 - Only allow edit of custom field once per customer on My account > edit account in WooCommerce WooCommerce -Thankyou 和“我的帐户”查看订单页面上的自定义通知 - WooCommerce - Custom notice on Thankyou and "My account" view order pages 在Woocommerce我的帐户页面中包含自定义模板文件问题 - Include custom template file issue in Woocommerce my account page 在 Woocommerce 编辑帐户页面中添加额外的自定义字段 - Adding an additional custom field in Woocommerce Edit Account page 在[woocommerce_my_account]的左侧添加自定义链接 - Adding a custom link to left of [woocommerce_my_account] 更改 WooCommerce 我的帐户客户订单的排序 - Change sorting of WooCommerce My account customer orders
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM