How to Get Various WooCommerce Page URLs

In an E-commerce site, we need Shop, Cart, Checkout & My Account, Logout and Payment page. So we install WooCommerce plugin, by default these plugins create all that pages but some point we want URL of this page for use in our WooCommerce plugins or themes. So what we do? Here is a solution. Here we find a solution to How to Get Various WooCommerce Page URLs(get_permalink).

Here, we are going to use one amongst 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. It will return -1 if no page is found.

We are also going to use one amongst 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.

1. My Account

$my_account_page_id  = wc_get_page_id( 'myaccount' );
$my_account_page_url = $my_account_page_id ? get_permalink( $my_account_page_id ) : ' ';

2. Shop

$shop_page_id = wc_get_page_id( 'shop' );
$shop_page_url = $shop_page_id ? get_permalink( $shop_page_id ) : ' ';

3. Payment

$payment_page_id = wc_get_page_id( 'pay' );
$payment_page_url = $payment_page_id ? get_permalink( $payment_page_id ) : ' ';

4. Cart

The WooCommerce Cart URL can be retrieved with a call to the cart object’s get_cart_url() method:

global $woocommerce;
$cart_page_url = function_exists( 'wc_get_cart_url' ) ? wc_get_cart_url() : $woocommerce->cart->get_cart_url();

Another Option:

$cart_page_id = wc_get_page_id( 'cart' );
$cart_page_url = $cart_page_id ? get_permalink( $cart_page_id ) : ' ';

5. Checkout

WooCommerce Checkout URL can be retrieved with a call to the cart object’s get_checkout_url() method:

global $woocommerce;
$checkout_page_url = function_exists( 'wc_get_cart_url' ) ? wc_get_checkout_url() : $woocommerce->cart->get_checkout_url();

Another Option :

$checkout_page_id = wc_get_page_id( 'checkout' );
$checkout_page_url = $checkout_page_id ? get_permalink( $checkout_page_id ) : ' ';

6. Logout

In this condition,  if your store has Force secure checkout setting enabled, which is found at the WooCommerce – Settings – Checkout tab then, we are going to check.. To add the “https:” protocol to your logout URL we have replaced the “http:” with “https:” which will make your logout URL secure.

$myaccount_page_id = get_option( 'woocommerce_myaccount_page_id' );
 if ( $myaccount_page_id ) {
  $logout_url = wp_logout_url( get_permalink( $myaccount_page_id ) );
 if ( get_option( 'woocommerce_force_ssl_checkout' ) == 'yes' )
    $logout_url = str_replace( 'http:', 'https:', $logout_url );
   }

I hope this post is useful and interesting.  you have any query related to WooCommerce than comment below. Our experienced WooCommerce developers are ready to solve your query and get the best support for any issues from eCommerce website

 

I am definitely sure that after reading this article you learn How to Get Various WooCommerce Page URLs