WooCommerce - Assign endpoints to multiple custom templates in my-account page

家住魔仙堡 提交于 2019-12-12 13:29:10

问题


I am trying to add 2 endpoints and associate them with two custom templates. 'my-server' -> 'Servers' and 'my-affiliate -> 'Affiliate'.

I also have created two custom templates:

  • my-server.php
  • my-affiliate.php

Both of them are located in my theme > woocommerce > myaccount folder. Affiliate page is pointing correctly to url/myaccount/my-affiliate.

But my problem is that Servers is giving "404 page not found" error.

I have tried to use the solution in this thread:
Assigning an endpoint to a custom template in my account pages
Ideally I should have requested this as an comment but I do not have sufficient reputation to comment. DarioFerrer 's resolution works great for a single endpoint and a single custom template.

In my case, I cannot figure out the solution for 2 or more endpoints:

  • How to include more than 2 endpoints?
  • How to assign each of them to custom templates?.

Any help will be greatly appreciated.

This is my functions.php code:

function my_custom_endpoints() {
    add_rewrite_endpoint( 'my-server', EP_ROOT | EP_PAGES );
    add_rewrite_endpoint( 'my-affiliate', EP_ROOT | EP_PAGES ); 
}
add_action( 'init', 'my_custom_endpoints' );

function my_custom_query_vars( $vars ) {
    $vars[]= 'my-server';
    $vars[] = 'my-affiliate';
    return $vars;
}

add_filter( 'query_vars', 'my_custom_query_vars', 0 );

function my_custom_my_account_menu_items( $items ) {
    $items = array(
        'dashboard'         => __( 'Dashboard', 'woocommerce' ),
        'my-server'         => __( 'Servers', 'woocommerce' ),
        'orders'            => __( 'Orders', 'woocommerce' ),
        //'downloads'       => __( 'Downloads', 'woocommerce' ),
        //'edit-address'    => __( 'Addresses', 'woocommerce' ),
        //'payment-methods' => __( 'Payment Methods', 'woocommerce' ),
        'edit-account'      => __( 'Edit Accounts', 'woocommerce' ),
        'my-affiliate'      => __( 'Affiliate', 'woocommerce' ),
        'customer-logout'   => __( 'Logout', 'woocommerce' ),
    );

    return $items;
}

add_filter( 'woocommerce_account_menu_items', 'my_custom_my_account_menu_items' );


function my_affiliate_endpoint_content() {
    include 'woocommerce/myaccount/my-affiliate.php';   
}
add_action( 'woocommerce_account_my-affiliate_endpoint', 'my_affiliate_endpoint_content' );

function my_server_endpoint_content() {
    include 'woocommerce/myaccount/my-server.php';  
}
add_action( 'woocommerce_account_my-server_endpoint', 'my_server_endpoint_content' );

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

I am using Wordpress 4.5.3 with Woocommerce 2.6.2 on Theme Cardinal (Premium theme by Swiftideas).
I am running this website on WAMP / localhost.
I am not using any affiiliate plugins. I have created both the custom templates with some general HTML content. Affiliate Tab presently does not have any Affiliate related content only just html for me to use it once every thing is set.

References:

  • Assigning an endpoint to a custom template in my account pages
  • Tabbed My Account page (Woocommerce 2.6+): Creating new endpoints

回答1:


Re-save your permalinks.

Any time you have 404s, It's a safe bet to re-save your permalinks. It can't hurt and solves a lot of problems. Presumably, you added the 2nd endpoint after switching themes because once I created some fake templates in the woocommerce folder, your code worked fine for me.

Sidenote

Please don't put this kind of functionality in a theme.
It'd be better in a plugin and then you can flush the permalinks on activation/deactivation.



来源:https://stackoverflow.com/questions/38266608/woocommerce-assign-endpoints-to-multiple-custom-templates-in-my-account-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!