WooCommerce: How to Get Shop URLs

Developing a WooCommerce website can be a pain if you don’t have a library of snippets to speed up the development process.

Since it is the season of giving (Xmas time) below is a bunch of handy snippets for any WordPress developers out there who are looking to add to their snippet libraries.

Get The Shop URL

Use the following PHP code to get the WooCommerce Shop URL (this is the root category page):

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

 

Get The My Account URL

You can get the WooCommerce My Account URL by using the woocommerce_myaccount_page_id option:

$myaccount_page_id = get_option( 'woocommerce_myaccount_page_id' );
if ( $myaccount_page_id ) {
  $myaccount_page_url = get_permalink( $myaccount_page_id );
}

 

Get The Cart URL

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

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

 

Get Current Page Slug

This snippet will get current page slug only (last part without your base URL):

global $wp;
$current_slug = add_query_arg( array(), $wp->request );

 

Get The Checkout URL

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

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

 

Get The Payment Page URL

This is the payment page URL used to collect payment information after the checkout page by redirect/hosted payment gateways. The path typically looks like /checkout/pay/. Get this URL with the following:

$payment_page_url = get_permalink( woocommerce_get_page_id( 'pay' ) );

// make ssl if needed
if ( get_option( 'woocommerce_force_ssl_checkout' ) == 'yes' ) $payment_page_url = str_replace( 'http:', 'https:', $payment_page_url );

 

Logout URL

This example will generate a WordPress logout URL that brings the user back to the Account area of the site:

$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 );
}

 

 

 

Posted on December 19, 2018 | Categories: Blog