| @@ -20,10 +20,10 @@ | ||
| 20 | 20 | return 0; |
| 21 | 21 | }; |
| 22 | 22 | |
| 23 | 23 | const updateButtonState = ($button, state) => { |
| 24 | - const labelDefault = settings.labels?.add || ''; | |
| 25 | - const labelAdded = settings.labels?.added || ''; | |
| 24 | + const labelDefault = $button.attr('data-label-default') || settings.labels?.add || ''; | |
| 25 | + const labelAdded = $button.attr('data-label-added') || settings.labels?.added || ''; | |
| 26 | 26 | const isAdded = state === 'added'; |
| 27 | 27 | |
| 28 | 28 | $button.attr('data-state', isAdded ? 'added' : 'default'); |
| 29 | 29 | $button.attr('aria-pressed', isAdded ? 'true' : 'false'); |
| @@ -28,8 +28,9 @@ | ||
| 28 | 28 | $button.attr('data-state', isAdded ? 'added' : 'default'); |
| 29 | 29 | $button.attr('aria-pressed', isAdded ? 'true' : 'false'); |
| 30 | 30 | $button.toggleClass('king-addons-wishlist-button--added', isAdded); |
| 31 | 31 | $button.toggleClass('king-addons-wishlist-button--default', !isAdded); |
| 32 | + $button.attr('aria-label', isAdded ? labelAdded : labelDefault); | |
| 32 | 33 | |
| 33 | 34 | const $label = $button.find('.king-addons-wishlist-button__label'); |
| 34 | 35 | if ($label.length) { |
| 35 | 36 | $label.text(isAdded ? labelAdded : labelDefault); |
| @@ -43,8 +44,47 @@ | ||
| 43 | 44 | $counter.text(count); |
| 44 | 45 | }); |
| 45 | 46 | }; |
| 46 | 47 | |
| 48 | + const restoreEmptyRows = () => { | |
| 49 | + $('.king-addons-wishlist-table tbody').each(function () { | |
| 50 | + const $tbody = $(this); | |
| 51 | + if ($tbody.find('.king-addons-wishlist-table__row').length) { | |
| 52 | + return; | |
| 53 | + } | |
| 54 | + if ($tbody.find('.king-addons-wishlist-table__empty').length) { | |
| 55 | + return; | |
| 56 | + } | |
| 57 | + const cols = $tbody.closest('table').find('thead th').length || 1; | |
| 58 | + $tbody.append( | |
| 59 | + '<tr><td colspan="' + cols + '" class="king-addons-wishlist-table__empty">' + | |
| 60 | + (settings.labels?.empty || 'No products in wishlist yet.') + | |
| 61 | + '</td></tr>' | |
| 62 | + ); | |
| 63 | + }); | |
| 64 | + }; | |
| 65 | + | |
| 66 | + const syncButtons = (productId, variationId, state) => { | |
| 67 | + $('.king-addons-wishlist-button').each(function () { | |
| 68 | + const $btn = $(this); | |
| 69 | + const pid = parseInt($btn.attr('data-product-id'), 10); | |
| 70 | + const vid = parseInt($btn.attr('data-variation-id'), 10) || 0; | |
| 71 | + if (pid !== productId || vid !== variationId) { | |
| 72 | + return; | |
| 73 | + } | |
| 74 | + if ($btn.hasClass('king-addons-wishlist-button--table-remove')) { | |
| 75 | + if (state === 'default') { | |
| 76 | + $btn.closest('tr').remove(); | |
| 77 | + } | |
| 78 | + return; | |
| 79 | + } | |
| 80 | + updateButtonState($btn, state); | |
| 81 | + }); | |
| 82 | + if (state === 'default') { | |
| 83 | + restoreEmptyRows(); | |
| 84 | + } | |
| 85 | + }; | |
| 86 | + | |
| 47 | 87 | const handleToggle = ($button) => { |
| 48 | 88 | const productId = parseInt($button.data('product-id'), 10); |
| 49 | 89 | const wishlistId = $button.data('wishlist-id') || ''; |
| 50 | 90 | const variationId = getVariationId($button); |
| @@ -63,15 +103,11 @@ | ||
| 63 | 103 | wishlist_id: wishlistId, |
| 64 | 104 | }) |
| 65 | 105 | .done((response) => { |
| 66 | 106 | if (response.success && response.data) { |
| 67 | - updateButtonState($button, response.data.state); | |
| 107 | + syncButtons(productId, variationId, response.data.state); | |
| 68 | 108 | if (response.data.count !== undefined) { |
| 69 | 109 | updateCounters(response.data.count); |
| 70 | - } | |
| 71 | - | |
| 72 | - if (response.data.state === 'default' && $button.hasClass('king-addons-wishlist-button--table-remove')) { | |
| 73 | - $button.closest('tr').remove(); | |
| 74 | 110 | } |
| 75 | 111 | } else if (response?.data?.message) { |
| 76 | 112 | alert(response.data.message); |
| 77 | 113 | } else { |