How to Get Checkout URL In WooCommerce

WooCommerce Get Checkout URLWhen customizing WooCommerce checkout page, you may want to get the checkout URL to create a redirect after WooCommerce checkout or to customize the checkout URL. You may also want to save the WooCommerce checkout URL as a variable to later pass it to your functions and so on.

In this case you need to know how to quickly get checkout URL in your code. In this simple and straightforward tutorial, I will show you how you can get the checkout URL with an code snippet and an example.

WooCommerce Get Checkout URL

From the global $woocommerce object you can get the checkout URL. This can be done using the avaialble function that allows users to get checkout URL.

The WooCommerce checkout URL can be pulled with a call to the cart object’s get_checkout_url() method. To get the checkout URL we need to access the global $woocommerce object.

If you do not declare the global object then we would be not able to get WooCommerce checkout URL. Secondly, we need to use 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 ) : '';

Conclusion

In this post, we have looked at how to get checkout URL, using the code snippet shared above. You can implement this code in your WooCommerce theme or plugin. If you need further assistance, feel free to get in touch with me.

Similar Articles