Cherry pick 51449 into release/9.3 (#51476)

* Check if button element exists when triggering added_to_cart (#51449)

* Prep for cherry pick 51449

---------

Co-authored-by: Sam Seay <samueljseay@gmail.com>
Co-authored-by: WooCommerce Bot <no-reply@woocommerce.com>
This commit is contained in:
github-actions[bot] 2024-09-18 19:34:12 +07:00 committed by GitHub
parent 68812a9c59
commit 589367e156
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 19 additions and 11 deletions

View File

@ -173,6 +173,8 @@ jQuery( function( $ ) {
* Update cart page elements after add to cart events. * Update cart page elements after add to cart events.
*/ */
AddToCartHandler.prototype.updateButton = function( e, fragments, cart_hash, $button ) { AddToCartHandler.prototype.updateButton = function( e, fragments, cart_hash, $button ) {
// Some themes and plugins manually trigger added_to_cart without passing a button element, which in turn calls this function.
// If there is no button we don't want to crash.
$button = typeof $button === 'undefined' ? false : $button; $button = typeof $button === 'undefined' ? false : $button;
if ( $button ) { if ( $button ) {
@ -222,19 +224,25 @@ jQuery( function( $ ) {
* Update cart live region message after add/remove cart events. * Update cart live region message after add/remove cart events.
*/ */
AddToCartHandler.prototype.alertCartUpdated = function( e, fragments, cart_hash, $button ) { AddToCartHandler.prototype.alertCartUpdated = function( e, fragments, cart_hash, $button ) {
var message = $button.data( 'success_message' ); // Some themes and plugins manually trigger added_to_cart without passing a button element, which in turn calls this function.
// If there is no button we don't want to crash.
$button = typeof $button === 'undefined' ? false : $button;
if ( !message ) { if ( $button ) {
return; var message = $button.data( 'success_message' );
}
if ( !message ) {
return;
}
// If the response after adding/removing an item to/from the cart is really fast, // If the response after adding/removing an item to/from the cart is really fast,
// screen readers may not have time to identify the changes in the live region element. // screen readers may not have time to identify the changes in the live region element.
// So, we add a delay to ensure an interval between messages. // So, we add a delay to ensure an interval between messages.
e.data.addToCartHandler.$liveRegion e.data.addToCartHandler.$liveRegion
.delay(1000) .delay(1000)
.text( message ) .text( message )
.attr( 'aria-relevant', 'all' ); .attr( 'aria-relevant', 'all' );
}
}; };
/** /**