How to Get WooCommerce Default Page URLs Dynamically
If you go to Pages > All Pages after WooCommerce installation, you will find the pages I’m talking about.
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
<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
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
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.
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.
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( '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
Filter name is
woocommerce_get_' . $page . '_page_permalink, so it can be used for the cart and checkout pages as well.
- My account
My account page ID is stored in wp_options under the key
My Account Endpoints
Just under the Checkout endpoints you will find Account endpoints.
Most of them you will find in My Account menu as subpages.
The thing is that we have a specific function which allows us to get those pages dynamically –
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:
dashboard– Dashboard, if you pass it, the function just returns
wc_get_page_permalink( 'myaccount' ),
view-order– View order,
edit-account– Edit account, by the way you can also use
payment-methods– Payment methods,
lost-password– Lost password,
customer-logout– Logout, when passed, the function just returns the result of
There is also
wc_lostpassword_url() function by the way.
The last but not least, the shop page.
wc_get_page_permalink( 'shop' );
Its ID in wp_options under the key