How to Get WooCommerce Page URLs » Cart, Shop,Checkout & Account

How to Get WooCommerce Page URLs programaticallyDo you want to get cart URL in WooCommerce? Recently, I was creating a redirect option for a custom WooCommerce plugin. I was required to get the cart URL and use it in the redirection code. In this project, I was required to redirect the WooCommerce user based on different situations. It is important as a WooCommerce developer, to learn how you can programmatically, get WooCommerce page URLs and use them in your code.

WooCommerce Page URLs

In this post, I am going to share with you how to get cart URL in WooCommerce as well as how to get page URL in WooCommerce. All the default WooCommerce pages are created during the installation process and they include:

  • My Account
  • Cart
  • Checkout
  • Shop

These pages come with their default endpoints that you can customize from the WooCommerce dashboard. These default pages are also clearly marked in the pages overview page with a clear indicator of the page as follows:

get woocommerce cart url

Change WooCommerce Checkout & MyAccount Page URLs

You can change the URLs of the pages since they are not set in the code. You can change these URL by editing the endpoints in the WooCommerce > Settings > Advanced and in the section named Checkout endpoints as shown on the image below:

woocommerce endpoints

There are also useful WooCommerce functions that you can use to get page URL in the code as you will see shortly.

#1) How to Get WooCommerce Shop URL

To get the shop URL in WooCommerce, you should use the following code:

$shop_page_url = get_permalink( woocommerce_get_page_id( 'shop' ) );

It basically grabs the shop page permalink using the id and bypassing the ‘shop’ page name. You can also use the wc_get_page_permalink() function to get the URL of the shop page as in this code snippet :

wc_get_page_permalink( 'shop' );

#2) How to Get WooCommerce Cart URL

The cart URL requires you declare the global WooCommerce object and then use the object to access the cart url using the wc_get_cart_url() function as in the code below :

global $woocommerce;
$cart_url = $woocommerce->cart->wc_get_cart_url();

Usage in Link

You can use the function wc_get_cart_url() to get the cart URL and use it in Ahref tag as follows :

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

The following is a code extract from the project I mentioned in the beginning of this post where I needed to add the cart and checkout buttons:

<a href='<?php echo wc_get_cart_url();?>' class='btn'><?php echo 'View Cart';?></a>

<a href='<?php echo wc_get_checkout_url();?>' class='btn'><?php echo 'Checkout';?></a>

Note how I have used the functions to add the cart page URLs to the Ahref tag and the following is the outcome of the two buttons code:

get woocommerce cart url

#3) How to Get Checkout Page URL

As I have already shared in the code snippet above, you can get the checkout page URL using the wc_get_checkout_url()

global $woocommerce;$checkout_url = $woocommerce->cart->wc_get_checkout_url()

Usage in Link

<a href='<?php echo wc_get_checkout_url();?>' class='btn'><?php echo 'Checkout';?></a>

You can also combine it with the Checkout endpoints and using the wc_get_endpoint_url() as  follows :

wc_get_endpoint_url( 'cool-endpoint', '', wc_get_checkout_url() );

 

You can change the WooCommerce checkout endpoints in the settings page WooCommerce > Settings > Advanced and in the section named Checkout endpoints as shown on the image below:

woocommerce endpoints

 #4) How to Get My Account Page URL

My Account page is an important page in WooCommerce user management, you can get the URL using the wc_get_page_permalink() function and passing the ‘my account’ slug as follows :

wc_get_page_permalink( 'myaccount' );

Conclusion

In this post, we have highlighted the various code snippets that you can use to get cart URL in WooCommerce as well as how to get the URL for all other default WooCommerce pages – my account, checkout, and shop. There are several ways you can employ this code in your project to create innovative solutions for your WooCommerce site users.