[英]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.