1

I need to know which endpoint I'm on from within My Account's navigation.php. I'd like to do something like:

if( is_wc_endpoint('my-endpoint-1') ) {
    echo "My Endpoint #1";
}
if( is_wc_endpoint('my-endpoint-2') ) {
    echo "My Endpoint #2";
}

or:

$endpoint = WC()->query->get_current_endpoint();
if( 'my-endpoint-1' === $endpoint ) ) {
    echo "My Endpoint #1";
}
if( 'my-endpoint-2' === $endpoint ) ) {
    echo "My Endpoint #2";
}

... but neither are working.

In the first example is_wc_endpoint('my-endpoint-1') is always false.

In the second example WC()->query->get_current_endpoint() is always empty.

In the existing WC templates the page title is displayed statically in each template after the navigation bar. For example, <h2>Edit Account</h2>. I need to display the endpoint title above the navigation bar which is outside of the endpoint content hook. So I have to determine the current endpoint and display the endpoint conditionally.

Also, existing WC endpoints work correctly with the above calls, but none of my custom endpoints work. Even calling is_wc_endpoint('my-endpoint-1') from one of my custom endpoints returns false.

I define my custom endpoints in functions.php with:

function add_my_account_endpoints() {
    add_rewrite_endpoint( 'properties', EP_ROOT | EP_PAGES );
    add_rewrite_endpoint( 'inbox', EP_ROOT | EP_PAGES );
    add_rewrite_endpoint( 'help', EP_ROOT | EP_PAGES );
    add_rewrite_endpoint( 'submit-ticket', EP_ROOT | EP_PAGES );
}
add_action( 'init', 'add_my_account_endpoints' );

There must be something different or additional required for custom endpoints? How do I know exactly which endpoint I'm on from within in one my custom endpoints?

  • 1
    is_wc_endpoint() is not a WooCommerce function. What does it do? is it this? – Jacob Peattie Oct 9 '17 at 3:17
  • @JacobPeattie Yes, I've seen several posts with that function in it. I thought it was a core WC function, but I see it's a custom function. Still, it doesn't work either. The function always returns false on the very first line of code in that function, I'm only wanting to know which custom endpoint is currently being displayed at any given time, but I'm not having any success with that. Thank you for pointing out that this is not a core function. :S – rwkiii Oct 9 '17 at 15:19
  • @JacobPeattie When on the dashboard endpoint wc_get_endpoint_url() returns http://..../my-account/ but when on one of my custom endpoints that function returns http://..../my-account// - note the double slashes at the end on my custom endpoint. It's like my custom endpoints aren't included in the URI so I can't even do a simple strpos() to check if I'm on a specific endpoint. – rwkiii Oct 9 '17 at 15:27
4

to use WC()->query->get_current_endpoint() you have to declare your new endpoints to Woocommerce with that :

add_filter("woocommerce_get_query_vars", function ($vars) {

    foreach (["properties", "inbox", "help", "submit-ticket"] as $e) {
        $vars[$e] = $e;
    }

    return $vars;

});
  • This works! Only on the dashboard endpoint WC()->query->get_current_endpoint() returns an empty string. I doubt if an empty string reliably indicates the dashboard? Is there another way of checking that you're on the root /my-account/ endpoint? Thx again! – rwkiii Oct 10 '17 at 0:34
  • The Woo breadcrumbs uses: if(is_account_page() && !is_wc_endpoint_url()) { // You are on the root } – Jonas Lundman Oct 15 '17 at 14:08

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.