From 554434ea3d366ce688c3d945c2e617cf83a32554 Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Tue, 17 Sep 2024 13:56:01 +0100 Subject: [PATCH] Cart Shortcode: `wc_get_cart_url` should only return current URL if on the cart page (#51384) * Narrow logic further by only checking if the current page is the cart, not WOOCOMMERCE_CART * changelog --- plugins/woocommerce/changelog/fix-cart-url-page-check-50524 | 4 ++++ plugins/woocommerce/includes/wc-core-functions.php | 6 +++++- 2 files changed, 9 insertions(+), 1 deletion(-) create mode 100644 plugins/woocommerce/changelog/fix-cart-url-page-check-50524 diff --git a/plugins/woocommerce/changelog/fix-cart-url-page-check-50524 b/plugins/woocommerce/changelog/fix-cart-url-page-check-50524 new file mode 100644 index 00000000000..dbd4b4aa4bc --- /dev/null +++ b/plugins/woocommerce/changelog/fix-cart-url-page-check-50524 @@ -0,0 +1,4 @@ +Significance: patch +Type: fix + +wc_get_cart_url should only return current URL if on the cart page. This excludes the usage of WOOCOMMERCE_CART. diff --git a/plugins/woocommerce/includes/wc-core-functions.php b/plugins/woocommerce/includes/wc-core-functions.php index 4158fd900e0..34d4781f00c 100644 --- a/plugins/woocommerce/includes/wc-core-functions.php +++ b/plugins/woocommerce/includes/wc-core-functions.php @@ -1484,7 +1484,11 @@ function wc_transaction_query( $type = 'start', $force = false ) { * @return string Url to cart page */ function wc_get_cart_url() { - if ( is_cart() && isset( $_SERVER['HTTP_HOST'], $_SERVER['REQUEST_URI'] ) ) { + // 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 ) ) || wc_post_content_has_shortcode( 'woocommerce_cart' ); + + if ( $is_cart_page && isset( $_SERVER['HTTP_HOST'], $_SERVER['REQUEST_URI'] ) ) { $protocol = is_ssl() ? 'https' : 'http'; $current_url = esc_url_raw( $protocol . '://' . wp_unslash( $_SERVER['HTTP_HOST'] ) . wp_unslash( $_SERVER['REQUEST_URI'] ) ); $cart_url = remove_query_arg( array( 'remove_item', 'add-to-cart', 'added-to-cart', 'order_again', '_wpnonce' ), $current_url );