1

I am working on a project where my account page is a homepage. So what I did I have created a page that holding the shortcode [woocommerce_my_account] and made this page homepage from Dashboard->Settings->Reading. Everything works. I have this working endpoints too:

  • mydomain.com/orders
  • mydomain.com/edit-address

But I am facing trouble when I am making a custom myaccount endpoint. I am doing it in this traditional way:

add_action('woocommerce_account_custom-endpoint_endpoint', function(){
    echo 'hello'; 
} );

add_action('init', function() {
    add_rewrite_endpoint('custom-endpoint', EP_ROOT | EP_PAGES); 
});

But mydomain.com/custom-endpoint is not pointing to my account page, it is pointing to the index.php or page.php (WordPress template hierarchy).

I am curious to know why it is happening?

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Jitu
  • 411
  • 2
  • 5
  • 13
  • hi. @VincenzoDiGaetano I think this is not similar. I have forgotten to mention if remove my account page from the homepage my custom endpoints are working okay. I mean mydomain.com/myaccount/custom-endpoint working okay but mydomain.com/custom-endpoint not working/ – Jitu Mar 19 '21 at 23:02
  • The `mydomain.com/custom-endpoint` page is not working but it is right. You are using the [**`'woocommerce_account_' . $key . '_endpoint'`**](https://woocommerce.github.io/code-reference/files/woocommerce-includes-wc-template-functions.html#source-view.3075) hook that adds the content within the WooCommerce "My Account" page. – Vincenzo Di Gaetano Mar 19 '21 at 23:20
  • mydomain.com is serving the myaccount page, so if add an endpoint with 'woocommerce_account_$key_endpoint', then should not mydomain.com/$key serve that custom endpoint as other default endpoints like mydomain.com/orders are working okay. but it is serving index.php – Jitu Mar 20 '21 at 00:04

1 Answers1

5

Updated:

Important: You need first to declare your home page as the My account page in:
WooCommerce settings > Advanced > My account page field.

Use the following:

// Enable endpoint
add_filter( 'woocommerce_get_query_vars', 'myaccount_custom_endpoint_query_var' );
function myaccount_custom_endpoint_query_var( $query_vars ) {
    $query_vars['custom-endpoint'] = 'custom-endpoint';

    return $query_vars;
}

// Endpoint displayed content
add_action('woocommerce_account_custom-endpoint_endpoint', 'display_custom_endpoint_content' ); 
function display_custom_endpoint_content(){
    echo '<p>' . __("hello") . '</p>';
}

Optionally you can use the following too:

// Add it as my account menu item
add_filter ( 'woocommerce_account_menu_items', 'custom_account_menu_items', 10 );
function custom_account_menu_items( $menu_links ){
    $menu_links = array_slice( $menu_links, 0,3 , true )
    + array( 'custom-endpoint' => __('Custom Endpoint') )
    + array_slice( $menu_links, 3, NULL, true );

    return $menu_links;
}

// Endpoint page title
add_filter( 'woocommerce_endpoint_custom-endpoint_title', 'set_my_account_custom_endpoint_title', 10, 2 );
function set_my_account_custom_endpoint_title( $title, $endpoint ) {
    $title = __( "Custom Endpoint", "woocommerce" );

    return $title;
}

Code goes in functions.php file of the active child theme (or active theme).

Edit: The hook woocommerce_get_query_vars is mandatory and replace the function that handle add_rewrite_endpoint(), which is not needed anymore (thanks to @Jitu).

You can flush rewrites rules if needed by going to Wordpress settings > Permalinks and "Save changes".

Tested and works.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Thanks, it is working, when I am using this hook 'woocommerce_get_query_vars'. Still wondering custom endpoints work okay without this hook when myaccount is not a root url. – Jitu Mar 20 '21 at 01:27
  • 1
    @Jitu Because wooCommerce default endpoints are already defined as query_vars in the core code, So for custom my account endpoints is good to do it, as for example the endpoint title will only be displayed if the query_var is defined. – LoicTheAztec Mar 20 '21 at 01:36
  • Thanks I got my answer. I have another question, is `add_rewrite_endpoint('custom-endpoint', EP_ROOT | EP_PAGES);` is required? I can see my custom endpoint works okay without this one when I use 'woocommerce_get_query_vars'. – Jitu Mar 20 '21 at 01:49
  • sorry I can not agree. I did that, also I am using `flush_rewrite_rules();` under `init` hook for testing, it is working okay. – Jitu Mar 20 '21 at 01:56
  • Also, I have deleted the .htaccess and regenerated it. it is still working. – Jitu Mar 20 '21 at 01:58
  • I did that, and I can clearly see `add_action('woocommerce_account_custom-endpoint_endpoint', function(){ echo 'Hello'; } );` `add_filter( 'woocommerce_get_query_vars',function ( $query_vars ) { $query_vars['custom-endpoint'] = 'custom-endpoint'; return $query_vars; });` is required things for me. although I will use `add_rewrite_endpoint ` because all tutorials suggest it. I will investigate more. – Jitu Mar 20 '21 at 02:07