wc_get_cart_url()WC 2.5.0

Gets the url to the cart page.

Hooks from the function

Return

String. Url to cart page

Usage

wc_get_cart_url();

Examples

0

#1 Display a link to the shopping cart

<a class="cart-link" href="<?php echo esc_url( wc_get_cart_url() ); ?>">
	View cart
</a>

Changelog

Since 2.5.0 Introduced.
Since 9.3.0 To support shortcodes on other pages besides the main cart page, this returns the current URL if it is the cart page.

wc_get_cart_url() code WC 9.5.1

function wc_get_cart_url() {
	global $post;

	// We don't use is_cart() here because that also checks for a defined constant. We are only interested in the page.
	$page_id      = wc_get_page_id( 'cart' );
	$is_cart_page = $page_id && is_page( $page_id );

	// If this isn't the cart page, but the page does contain the cart shortcode, we'll return the current page permalink.
	if ( ! $is_cart_page && is_a( $post, 'WP_Post' ) && wc_post_content_has_shortcode( 'woocommerce_cart' ) ) {
		$cart_url = get_permalink( $post->ID );
	} else {
		$cart_url = wc_get_page_permalink( 'cart' );
	}

	/**
	 * Filter the cart URL.
	 *
	 * @since 2.5.0
	 * @param string $cart_url Cart URL.
	 */
	return apply_filters( 'woocommerce_get_cart_url', $cart_url );
}