How to Get WooCommerce Default Page URLs Dynamically

When you install WooCommerce plugin, it automatically creates system pages – Cart, Checkout, My Account, Shop and a bunch of endpoints. In this tutorial I will show you the correct way to get URLs of these pages.

If you go to Pages > All Pages after WooCommerce installation, you will find the pages I’m talking about.

The WooCommerce default pages – Cart, Checkout, Shop, My Account

The urls of these pages are not hardcoded (so a WooCommerce shop administrator can change them, for example it is possible to rename “http://domain/cart” to “http://domain/basket”. Luckily there are a couple functions in WooCommerce that allow to get these page urls dynamically.

How to Get Cart URL

You can easily get the URL of your store cart page dynamically with wc_get_cart_url() function.

<a href="<?php echo wc_get_cart_url() ?>">Cart</a>

This function doesn’t have any parameters and works on the base of wc_get_page_permalink( 'cart' ), which also works on the base of wc_get_page_id( 'cart' ) which retrieves the cart page ID from the options with the key woocommerce_cart_page_id.

There is also a filter hook woocommerce_get_cart_url with the only parameter – the cart URL the function returns.

How to Get a Checkout Page URL

There is a similar situation with the checkout page. You can use wc_get_checkout_url() function for that, which doesn’t accept any parameters and works on the base of wc_get_page_permalink( 'checkout' ), which also works on the base of wc_get_page_id( 'checkout' ) which retrieves the checkout page ID from the options with the key woocommerce_checkout_page_id.

wc_get_checkout_url();

The function also has a hook inside it woocommerce_get_checkout_url, so you can filter the result URL everywhere on the store.

There is also an SSL check inside it. If the option Force SSL is enabled, it adds https to the url.

Checkout Endpoints

If you go to WooCommerce > Settings > Advanced, you will find out the Checkout endpoints. Not sure how to explain it the best – it means, that there are some virtual pages that work on base of the checkout page code. These pages have the same URL as the checkout page but an additional endpoint is added to the end of the checkout page URL.

WooCommerce checkout page endpoints

And you do not have to do it manually like wc_get_checkout_url() . 'add-payment-method' because you will get 404 error if the above settings will be changed.

There is a special function for this purpose wc_get_endpoint_url(). The correct usage for the Add payment method page is:

wc_get_endpoint_url( 'add-payment-method', '', wc_get_checkout_url() );

So the first parameter is the default endpoint name, you can find it of the screenshot by the way, the second parameter is usually empty and the third param is the URL we are going to add endpoint to.

My Account URL

My account page doesn’t have a specific function, so you can only use wc_get_page_permalink().

wc_get_page_permalink( 'myaccount' );

You’ve been looking for a filter for this page, don’t be sad, because you still can use a filter which is a part of wc_get_page_permalink() function.

Filter name is woocommerce_get_' . $page . '_page_permalink, so it can be used for the cart and checkout pages as well.

My account page ID is stored in wp_options under the key woocommerce_myaccount_page_id.

My Account Endpoints

Just under the Checkout endpoints you will find Account endpoints.

WooCommerce my account endpoints

Most of them you will find in My Account menu as subpages.

WooCommerce Default My Account menu.

The thing is that we have a specific function which allows us to get those pages dynamically – wc_get_account_endpoint_url().

You may think – ok, what is the problem, I can add it manually to the URL like this: wc_get_page_permalink( 'myaccount' ) . 'orders'. Please do not! 📛 Because in that case if the endpoint name will be changed in settings, you will get 404! So the correct way:

wc_get_account_endpoint_url( 'orders' );

So the function accepts the default endpoint names:

There is also wc_lostpassword_url() function by the way.

Shop Page

The last but not least, the shop page.

wc_get_page_permalink( 'shop' );

Its ID in wp_options under the key woocommerce_shop_page_id.

Related stuff

Misha Rudrastyh

Misha Rudrastyh

I love WordPress, WooCommerce and Gutenberg so much. 10 yrs of experience.

Need some custom developer help? Let me know

Follow Misha

Need some help with WooCommerce?

If you need some professional developer help, I will be happy to assist you.

Contact me Who I am?

Leave a comment

php js HTML CSS Code