Cart Shortcode: Fix cart rendering after AJAX updates by modifying cart page URL (#50524)

* Remove redirects in form handler if no referrer is passed

* Use current URL for cart URL if on the cart page

* Changelog

* Indents
This commit is contained in:
Mike Jolley 2024-08-14 15:31:37 +01:00 committed by GitHub
parent a932ceb59f
commit 1d9592db87
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 37 additions and 19 deletions

View File

@ -0,0 +1,4 @@
Significance: patch
Type: fix
Fix cart shortcode updates when not used on the main cart page.

View File

@ -582,7 +582,6 @@ class WC_Form_Handler {
wp_safe_redirect( wc_get_account_endpoint_url( 'payment-methods' ) );
exit();
}
}
/**
@ -607,7 +606,6 @@ class WC_Form_Handler {
wp_safe_redirect( wc_get_account_endpoint_url( 'payment-methods' ) );
exit();
}
}
/**
@ -653,10 +651,10 @@ class WC_Form_Handler {
wc_add_notice( $removed_notice, apply_filters( 'woocommerce_cart_item_removed_notice_type', 'success' ) );
}
$referer = wp_get_referer() ? remove_query_arg( array( 'remove_item', 'add-to-cart', 'added-to-cart', 'order_again', '_wpnonce' ), add_query_arg( 'removed_item', '1', wp_get_referer() ) ) : wc_get_cart_url();
wp_safe_redirect( $referer );
exit;
if ( wp_get_referer() ) {
wp_safe_redirect( remove_query_arg( array( 'remove_item', 'add-to-cart', 'added-to-cart', 'order_again', '_wpnonce' ), add_query_arg( 'removed_item', '1', wp_get_referer() ) ) );
exit;
}
} elseif ( ! empty( $_GET['undo_item'] ) && isset( $_GET['_wpnonce'] ) && wp_verify_nonce( $nonce_value, 'woocommerce-cart' ) ) {
// Undo Cart Item.
@ -664,10 +662,10 @@ class WC_Form_Handler {
WC()->cart->restore_cart_item( $cart_item_key );
$referer = wp_get_referer() ? remove_query_arg( array( 'undo_item', '_wpnonce' ), wp_get_referer() ) : wc_get_cart_url();
wp_safe_redirect( $referer );
exit;
if ( wp_get_referer() ) {
wp_safe_redirect( remove_query_arg( array( 'undo_item', '_wpnonce' ), wp_get_referer() ) );
exit;
}
}
// Update Cart - checks apply_coupon too because they are in the same form.
@ -722,9 +720,11 @@ class WC_Form_Handler {
exit;
} elseif ( $cart_updated ) {
wc_add_notice( __( 'Cart updated.', 'woocommerce' ), apply_filters( 'woocommerce_cart_updated_notice_type', 'success' ) );
$referer = remove_query_arg( array( 'remove_coupon', 'add-to-cart' ), ( wp_get_referer() ? wp_get_referer() : wc_get_cart_url() ) );
wp_safe_redirect( $referer );
exit;
if ( wp_get_referer() ) {
wp_safe_redirect( remove_query_arg( array( 'remove_coupon', 'add-to-cart' ), wp_get_referer() ) );
exit;
}
}
}
}

View File

@ -1469,12 +1469,26 @@ function wc_transaction_query( $type = 'start', $force = false ) {
/**
* Gets the url to the cart page.
*
* @since 2.5.0
* @since 2.5.0
* @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.
*
* @return string Url to cart page
*/
function wc_get_cart_url() {
return apply_filters( 'woocommerce_get_cart_url', wc_get_page_permalink( 'cart' ) );
if ( is_cart() && isset( $_SERVER['HTTP_HOST'], $_SERVER['REQUEST_URI'] ) ) {
$protocol = is_ssl() ? 'https' : 'http';
$cart_url = esc_url_raw( $protocol . '://' . wp_unslash( $_SERVER['HTTP_HOST'] ) . wp_unslash( $_SERVER['REQUEST_URI'] ) );
} 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 );
}
/**
@ -1614,7 +1628,7 @@ function wc_get_wildcard_postcodes( $postcode, $country = '' ) {
$formatted_postcode . '*',
);
for ( $i = 0; $i < $length; $i ++ ) {
for ( $i = 0; $i < $length; $i++ ) {
$postcodes[] = ( function_exists( 'mb_substr' ) ? mb_substr( $formatted_postcode, 0, ( $i + 1 ) * -1 ) : substr( $formatted_postcode, 0, ( $i + 1 ) * -1 ) ) . '*';
}
@ -1709,7 +1723,7 @@ function wc_get_shipping_method_count( $include_legacy = false, $enabled_only =
foreach ( $methods as $method ) {
if ( isset( $method->enabled ) && 'yes' === $method->enabled && ! $method->supports( 'shipping-zones' ) ) {
$method_count++;
++$method_count;
}
}
}
@ -2266,7 +2280,7 @@ function wc_get_var( &$var, $default = null ) {
*/
function wc_enable_wc_plugin_headers( $headers ) {
if ( ! class_exists( 'WC_Plugin_Updates' ) ) {
include_once dirname( __FILE__ ) . '/admin/plugin-updates/class-wc-plugin-updates.php';
include_once __DIR__ . '/admin/plugin-updates/class-wc-plugin-updates.php';
}
// WC requires at least - allows developers to define which version of WooCommerce the plugin requires to run.
@ -2301,7 +2315,7 @@ function wc_prevent_dangerous_auto_updates( $should_update, $plugin ) {
}
if ( ! class_exists( 'WC_Plugin_Updates' ) ) {
include_once dirname( __FILE__ ) . '/admin/plugin-updates/class-wc-plugin-updates.php';
include_once __DIR__ . '/admin/plugin-updates/class-wc-plugin-updates.php';
}
$new_version = wc_clean( $plugin->new_version );