0

I want to add a sub endpoint to custom endpoint defined for my woocommerce myaccount template. For example :

custom endpoint - my-account/alumni
subendpoint - my-account/alumni/index

I have added the functionality to add the custom endpoint but when i am trying to add the sub-endpoint it is redirecting me to my-account/ and not loading the template

I have already tried refreshing the permalinks. and changing the position of endpoint and sub-endpoint definitions. I have written this code in functions.php

add_action( 'init', 'add_alumni_index_account_endpoint' );
function add_alumni_index_account_endpoint() {
    add_rewrite_endpoint( 'alumni-portal/alumni-index', EP_PAGES );
}

add_action( 'woocommerce_account_alumni-index_endpoint', 'alumni_index_account_endpoint_content' );
function alumni_index_account_endpoint_content() {
    //if ( current_user_can('administrator') ) {
        wc_get_template( 'myaccount/alumni-index.php' );
    //}
}

add_action( 'init', 'add_alumni_portal_account_endpoint' );
function add_alumni_portal_account_endpoint() {
    add_rewrite_endpoint( 'alumni-portal', EP_PAGES );
}

add_filter ( 'woocommerce_account_menu_items', 'custom_account_menu_items', 10 );
function custom_account_menu_items( $menu_links ){
    //if ( current_user_can('administrator') ) {
        $menu_links = array_slice( $menu_links, 0,3 , true )
        + array( 'alumni-portal' => __('Alumni Portal') )
        + array_slice( $menu_links, 3, NULL, true );
    //}
    return $menu_links;
}

add_action( 'woocommerce_account_alumni-portal_endpoint', 'alumni_portal_account_endpoint_content' );
function alumni_portal_account_endpoint_content() {
    //if ( current_user_can('administrator') ) {
        wc_get_template( 'myaccount/alumni-portal.php' );
    //}
}

add_filter("woocommerce_get_query_vars", function ($vars) {
    foreach (["alumni-portal/alumni-index","alumni-portal"] as $e) {
        $vars[$e] = $e;
    }
    return $vars;
});

Result should be that my sub-endpoint should load its respective template rather than redirecting to my-account.

alumni-index.php has a simple code of hello

<?php
    if ( ! defined( 'ABSPATH' ) ) {
        exit;
    }
?>
<p>hello</p>
0

You can't add alumni-portal/alumni-index as a separate endpoint, because this clashes with the alumni-portal endpoint. This is because rewrite endpoints will capture anything after / as their value. So alumni-portal/alumni-index is the same as alumni-portal, but the alumni-portal query var is set to alumni-index.

So rather than registering both endpoints, register alumni-portal a single endpoint, and then check for which value for alumni-portal is passed. If the value is index, then display the appropriate template.

So register your endpoint with the woocommerce_get_query_vars filter:

add_filter( 
    'woocommerce_get_query_vars', 
    function( $query_vars ) {
        $query_vars['alumni'] = 'alumni';

        return $query_vars;
    }
);

Then use the woocommerce_account_alumni_endpoint action to display the page. These account endpoint hooks will pass the "value" of the endpoint as an argument to the callback function. So if I visit my-account/alumni/index the value passed will be index, and you can use this to display the appropriate template:

add_action( 
    'woocommerce_account_alumni_endpoint',
    function( $value ) {
        if ( $value === 'index' ) {
            wc_get_template( 'myaccount/alumni-index.php' );
        } else {
            wc_get_template( 'myaccount/alumni-portal.php' );
        }
    }
);

If you're filtering the account page title you don't get the value passed, but since you do get the endpoint you can get this with get_query_var():

add_filter( 
    'woocommerce_endpoint_alumni_title',
    function( $title, $endpoint ){
        $value = get_query_var( $endpoint );

        if ( $value === 'index' ) {
            $title = 'Alumni Index';
        } else {
            $title = 'Alumni Portal';
        }

        return $title;
    },
    10,
    2
);
  • Hey, thanks a lot. It worked! @jacob – user172501 Jul 27 at 3:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy