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

This commit is contained in:
Sam Seay 2024-09-18 10:13:47 +08:00 committed by GitHub
parent cf7fd8303c
commit 2e3013555e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 23 additions and 11 deletions

View File

@ -0,0 +1,4 @@
Significance: patch
Type: fix
Fix bug where manually triggering `added_to_cart` event without a button element caused an Exception.

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,6 +224,11 @@ 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 ) {
// 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 ( $button ) {
var message = $button.data( 'success_message' ); var message = $button.data( 'success_message' );
if ( !message ) { if ( !message ) {
@ -235,6 +242,7 @@ jQuery( function( $ ) {
.delay(1000) .delay(1000)
.text( message ) .text( message )
.attr( 'aria-relevant', 'all' ); .attr( 'aria-relevant', 'all' );
}
}; };
/** /**