How to Get Default Page URLs
If you go to Pages > All Pages after WooCommerce installation, you will notice that a couple more pages appeared there. These are kind of system pages in WooCommerce and required to provide your shop visitors proper eCommerce user experience.
Sometimes, and I might say – very often, when coding something for WooCommerce we need to get these pages URLs programmatically. We can’t use something like
site_url( 'cart' ), because website administrators allowed to change the system pages URLs, for example to use
example.com/basket instead of
example.com/cart and so on.
Luckily there are a couple functions in WooCommerce that allow to get these page URLs dynamically.
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.
Get 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
<a href="<?php echo wc_get_checkout_url() ?>">Checkout</a>
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:
<?php $url = wc_get_endpoint_url( 'add-payment-method', '', wc_get_checkout_url() ); ?> <a href="<?php echo $url ?>">Add Payment Method</a>
So the first parameter is the default endpoint name, you can find it on the screenshot by the way, the second parameter is usually empty and the third parameter is the URL we are going to add endpoint to.
Get Account Page URL
My account page doesn’t have a specific function, so you can only use
<a href="<?php echo wc_get_page_permalink( 'myaccount' ) ?>">My account</a>
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:
$orders_url = 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.
Get Shop Page URL
The last but not least, the shop page.
<a href="<?php echo wc_get_page_permalink( 'shop' ) ?>">Continue shopping</a>
Its ID in
wp_options under the key
Hey guys and welcome to my website. For more than 10 years I've been doing my best to share with you some superb WordPress guides and tips for free.
Need some developer help? Contact me
This is the best and most comprehensive article in this topic.