Table of Contents
When we install WooCommerce, it creates some pages that are needed for your ecommerce site to work. Those pages are Shop, Cart, Checkout & My Account pages. At some point, you will need the URL of these pages to use in your WooCommerce plugin or WooCommerce theme. In this post, we will see how to get different WooCommerce page URLs.
Here is the list of the pages which we will see.
- Shop
- Cart
- Checkout
- My Account
- Logout
- Payment
Here, we will use one of the WooCommerce function wc_get_page_id(), which gives us the id of the given parameter. The parameter in this function is the name of the page, which will return the id.
We have also used the WordPress function get_permalink() which will give us the URL of the specified parameters. The parameter in this function is the id of the page, which will return the URL.
These functions are used in almost every URL fetching part below. Along with that, some other methods are also mentioned in the post.
1. WooCommerce get shop page URL
To fetch the Shop page URL we use the WooCommerce function along with the WordPress function.
$shop_page_id = wc_get_page_id( 'shop' );
$shop_page_url = $shop_page_id ? get_permalink( $shop_page_id ) : '';
2. WooCommerce get cart page URL
Here, in this case, we can fetch the Cart page URL using two methods.
2.1 From the global $woocommerce variable
The WooCommerce Cart URL can be fetched with a call to the cart object’s get_cart_url() method. To fetch the cart URL we need global $woocommerce variable. If it is not declared then we would be not able to fetch it. Also, we have used the WooCommerce function wc_get_cart_url(), which is introduced after WooCommerce version 2.5.
global $woocommerce;
$cart_page_url = function_exists( 'wc_get_cart_url' ) ? wc_get_cart_url() : $woocommerce->cart->get_cart_url();
Note: The get_cart_url() cart object method has been deprecated since WooCommerce 2.5.
2.2 From the WooCommerce and WordPress function
$cart_page_id = wc_get_page_id( 'cart' );
$cart_page_url = $cart_page_id ? get_permalink( $cart_page_id ) : '';
3. WooCommerce get checkout URL
Similar to the cart page, we can fetch the checkout page URL using two methods.
3.1 From the global $woocommerce variable
The WooCommerce Cart URL can be fetched with a call to the cart object’s get_checkout_url() method. To fetch the checkout URL we need global $woocommerce variable. If it is not declared then we would be not able to fetch it. Also, we have used the WooCommerce function wc_get_checkout_url() which is introduced after WooCommerce version 2.5.
global $woocommerce;
$checkout_page_url = function_exists( 'wc_get_cart_url' ) ? wc_get_checkout_url() : $woocommerce->cart->get_checkout_url();
Note: The get_checkout_url() cart object method has been deprecated since WooCommerce 2.5.
3.2 From the WooCommerce and WordPress function
$checkout_page_id = wc_get_page_id( 'checkout' );
$checkout_page_url = $checkout_page_id ? get_permalink( $checkout_page_id ) : '';
We have fetched the Cart & Checkout page URL in our Abandoned Cart Pro for WooCommerce plugin using the above code as mentioned. This plugin sends the abandoned cart reminder emails to the customers who have abandoned the cart. To bring customers back to the cart/checkout page we have given 2 merge codes in our plugin {{cart.link}} & {{checkout.link}}.
Read Related Article: How to Change the URL for ‘Return to Shop’ in WooCommerce?
4. WooCommerce get account page URL
To fetch the My Account page URL we will again use the WooCommerce function along with the WordPress function.
$my_account_page_id = wc_get_page_id( 'myaccount' );
$my_account_page_url = $my_account_page_id ? get_permalink( $my_account_page_id ) : '';
5. WooCommerce logout URL
To fetch the Logout URL of the store we will use WordPress function wp_logout_url().
$logout_url = wp_logout_url( );
if ( get_option( 'woocommerce_force_ssl_checkout' ) == 'yes' ) {
$logout_url = str_replace( 'http:', 'https:', $logout_url );
}
The above condition will check if your store has Force secure checkout setting enabled, which is located at the WooCommerce – Settings – Checkout tab. To add the “https:” protocol to your logout URL we have replaced the “http:” with “https:” which will make your logout URL secure.
If you want to redirect your customer to a specific page after they logout from the website then you do as follow:
$logout_url = wp_logout_url( $my_account_page_url );
if ( get_option( 'woocommerce_force_ssl_checkout' ) == 'yes' ) {
$logout_url = str_replace( 'http:', 'https:', $logout_url );
}
In above case, once the customer is successfully logged-out from the website then it will redirect them to My Account page. You need to pass the URL as a parameter to the wp_logout_url() function.
6. WooCommerce payment URL
The payment page URL is used to collect the payment information after the checkout page using redirect/hosted payment gateways. The path typically looks like /checkout/pay/.
$payment_page_id = wc_get_page_id( 'pay' );
$payment_page_url = $payment_page_id ? get_permalink( $payment_page_id ) : '';
Drop me a line if you have any questions.