As you know woocommerce is the most popular platform for e-commerce websites. You can build your online store to sell anything.
There is a wide range of woocommerce communities and a deep explanation of documentation. If you want to enhance your store by adding new features then you can easily add them. You can use the woocommerce action hooks to customize the woocommerce functionality.
To enhance the woocommerce functionality, you may need the woocommerce default page URL like cart page, checkout page, shop page, and my account page.
So in this tutorial, we will see how to get woocommerce cart, checkout, shop and my account page URL dynamically using the woocommerce functions.
WooCommerce Default Pages
The following list is the default woocommerce pages.
- Cart
- Checkout
- Shop
- My Account
These pages are automatically created when you install the woocommerce plugin. You can change it as you want. Suppose you want to change the Cart to Basket then you can easily do this, but you have to tell woocommerce this is the new cart page in the woocommerce settings.
You can also use the built-in shortcode for these pages like
[woocommerce_cart]
, [woocommerce_checkout]
, [woocommerce_myaccount]
.
In some cases, you want to get these pages’ URLs dynamically in your code functionality then you can get it using the woocommerce functions.
Suppose you are making the functionality redirect to checkout after clicking on add to cart button then here you may need the checkout page URL.
So let’s see how we can get the default woocommerce pages URL (cart, page, checkout page, shop page, and my account page).
Get WooCommerce Cart Page URL
To get the woocommerce cart page URL, you can use the wc_get_cart_url()
function in your code where you want to show the cart page URL or make a cart link.
You can call this function directly, you don’t need to pass any parameter in this function. This function works on the basis of wc_get_page_permalink(‘cart’), which is also work on the base of wc_get_page_id(‘cart’).
See Also: How to Get WooCommerce Cart Items Details?
Get WooCommerce Checkout Page URL
To get the woocommerce checkout page URL, you can use the wc_get_checkout_url()
function. You just have to call this function with echo and it prints the checkout page URL.
This function also works on the same basis like cart page but have the ‘checkout‘ parameter like wc_get_page_permalink('checkout')
and it also base of wc_get_page_id('checkout')
. You don’t need to pass any parameter in this function to use it, just simply call it where you want.
See Also: Custom Validation in WooCommerce Checkout Form
Checkout Endpoints
There are also have woocommerce checkout endpoints that you can also get URL of them, but you cannot access them directly. Because these endpoints have different purposes to show the result based on the checkout page and response.
See the following image for the checkout endpoints in woocommerce settings under the Advanced tab.
If you will try to get these endpoints URL using the wc_get_checkout_url()
function like wc_get_checkout_url() . ‘order-pay’. Then you will get the error when you have changed the seeting in case.
To get the checkout endpoints URL, you can use the wc_get_endpoint_url()
function along with passing the endpoint name as a parameter. See the following example code for reference.
In this line of code, the first parameter is the name of the endpoint, the second parameter is blank and the third parameter is the checkout page URL.
This function also has filter hook woocommerce_get_checkout_url
which you can use for customization.
Get WooCommerce Shop Page URL
If you want to get the woocommerce shop page URL dynamically then you can use the wc_get_page_permalink()
functions. See the following code example.
You can also use the get_permalink()
and wc_get_page_id()
function to get shop page URL. You have to pass the shop page slug as a parameter in the wc_get_page_id() function and then pass this function into get_permalink() function. It will give you a shop page URL.
If you want to change the shop page URL then you can change it in the woocommerce settings under the Products tab. There is an option to select the woocommerce shop page, so select here. Then woocommerce will treat that page as a shop page.
See Also: How to Hide Specific Product From Shop Page?
Get WooCommerce My Account Page URL
To get the woocommerce my account page URL, then you can use the wc_get_page_permalink()
function with passing the ‘myaccount slug as a parameter.
My Account Endpoints
As you know there are my account endpoints to navigate the pages on like customers orders, view order, edit account, payment methods, etc. See the following image for reference.
So if you also want to get these endpoints URLs then you have to use the wc_get_account_endpoint_url()
function. You just have to pass the endpoint name as a parameter in the function.
Thus, you can use this endpoint function with the endpoint name to get my account endpoint page URL dynamically.
See Also: How to Get WooCommerce Order Details?
Bonus: WooCommerce Default Pages Filter Hook
There are also filter hooks for the woocommerce cart, checkout, my account page and you can use them in your code for customization.
Filter name is woocommerce_get_' . $page . '_page_permalink
, so you can use this hook for the pages as follows.
- Cart:
woocommerce_get_cart_page_permalink
- Checkout:
woocommerce_get_checkout_page_permalink
- My account:
woocommerce_get_myaccount_page_permalink
Conclusion
So in this tutorial, you learned how to get the woocommerce cart, checkout, shop, and my account page URL dynamically using the woocommerce functions. To change these page URLs, you need to change the setting options in the woocommerce settings.
You also learned about how to get the checkout endpoints URL and my account endpoints page URL using the wc_get_endpoint_url() and wc_get_myaccount_endpoint_url() functions. You just have to pass the endpoint name that you had set up in the woocommerce settings.