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

Pau*_*aul 5 php account wordpress endpoint woocommerce

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

URL被称为:

/my-account/edit-order/55/
Run Code Online (Sandbox Code Playgroud)

这在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;
}
Run Code Online (Sandbox Code Playgroud)

谢谢你的帮助。

Loi*_*tec 5

这是一个工作的解决方案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'; 
}
Run Code Online (Sandbox Code Playgroud)

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

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;
}
Run Code Online (Sandbox Code Playgroud)

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

  • 转到“永久链接”选项页面,然后重新保存永久链接(感谢 helgatheviking
  • 您还可以禁用/启用主题。

参考文献:

  • 要刷新重写规则,您还可以转到永久链接选项页面并重新保存永久链接。 (3认同)