Do not show "Adding new attribute failed" error message when loading of product screens is interrupted by page unload (#38815)

* Suppress "adding new attribute failed" error message on page unload
* Only attempt to add empty attribute if page has attributes list
This commit is contained in:
Matt Sherman 2023-06-21 21:26:01 -04:00 committed by GitHub
parent 72a6f2b503
commit 6076458471
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 21 deletions

View File

@ -0,0 +1,4 @@
Significance: patch
Type: fix
Do not show "Adding new attribute failed" error message when loading of product screens is interrupted by page unload.

View File

@ -1,5 +1,11 @@
/*global woocommerce_admin_meta_boxes */
jQuery( function ( $ ) {
let isPageUnloading = false;
$( window ).on( 'beforeunload', function () {
isPageUnloading = true;
} );
// Scroll to first checked category
// https://github.com/scribu/wp-category-checklist-tree/blob/d1c3c1f449e1144542efa17dde84a9f52ade1739/category-checklist-tree.php
$( function () {
@ -432,28 +438,27 @@ jQuery( function ( $ ) {
} );
} );
// Attribute Tables.
// Set up attributes, if current page has the attributes list.
const $product_attributes = $( '.product_attributes' );
if ( $product_attributes.length === 1 ) {
var woocommerce_attribute_items = $product_attributes.find( '.woocommerce_attribute' ).get();
// Initial order.
var woocommerce_attribute_items = $( '.product_attributes' )
.find( '.woocommerce_attribute' )
.get();
// If the product has no attributes, add an empty attribute to be filled out by the user.
$( function add_blank_custom_attribute_if_no_attributes() {
if ( woocommerce_attribute_items.length === 0 ) {
// If the product has no attributes, add an empty attribute to be filled out by the user.
if ( woocommerce_attribute_items.length === 0 ) {
add_custom_attribute_to_list();
}
} );
woocommerce_attribute_items.sort( function ( a, b ) {
var compA = parseInt( $( a ).attr( 'rel' ), 10 );
var compB = parseInt( $( b ).attr( 'rel' ), 10 );
return compA < compB ? -1 : compA > compB ? 1 : 0;
} );
$( woocommerce_attribute_items ).each( function ( index, el ) {
$( '.product_attributes' ).append( el );
} );
// Sort the attributes by their position.
woocommerce_attribute_items.sort( function ( a, b ) {
var compA = parseInt( $( a ).attr( 'rel' ), 10 );
var compB = parseInt( $( b ).attr( 'rel' ), 10 );
return compA < compB ? -1 : compA > compB ? 1 : 0;
} );
$( woocommerce_attribute_items ).each( function ( index, el ) {
$product_attributes.append( el );
} );
}
function update_attribute_row_indexes() {
$( '.product_attributes .woocommerce_attribute' ).each( function (
@ -571,9 +576,13 @@ jQuery( function ( $ ) {
disable_or_enable_fields();
jQuery.maybe_disable_save_button();
} catch ( error ) {
alert(
woocommerce_admin_meta_boxes.i18n_add_attribute_error_notice
);
if ( isPageUnloading ) {
// If the page is unloading, the outstanding ajax fetch may fail in Firefox (and possible other browsers, too).
// We don't want to show an error message in this case, because it was caused by the user leaving the page.
return;
}
alert( woocommerce_admin_meta_boxes.i18n_add_attribute_error_notice );
throw error;
} finally {
unblock_attributes_tab_container();