| 1 |
/** |
| 2 |
* Keep header/block mini-cart in sync after classic [woocommerce_cart] updates. |
| 3 |
*/ |
| 4 |
(function ($) { |
| 5 |
'use strict'; |
| 6 |
|
| 7 |
let refreshing = false; |
| 8 |
|
| 9 |
const refreshMiniCart = () => { |
| 10 |
if (refreshing) { |
| 11 |
return; |
| 12 |
} |
| 13 |
refreshing = true; |
| 14 |
|
| 15 |
document.dispatchEvent(new CustomEvent('wc-blocks_added_to_cart', { bubbles: true })); |
| 16 |
document.dispatchEvent(new CustomEvent('wc-blocks_removed_from_cart', { bubbles: true })); |
| 17 |
|
| 18 |
if (window.wp && wp.data && wp.data.dispatch) { |
| 19 |
try { |
| 20 |
const cartStore = wp.data.dispatch('wc/store/cart'); |
| 21 |
if (cartStore && typeof cartStore.invalidateResolution === 'function') { |
| 22 |
cartStore.invalidateResolution('getCartData'); |
| 23 |
} |
| 24 |
} catch (e) { |
| 25 |
// Store may be unavailable on pages without WooCommerce blocks. |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
if ($ && $.fn) { |
| 30 |
$(document.body).trigger('wc_fragment_refresh'); |
| 31 |
} |
| 32 |
|
| 33 |
window.setTimeout(() => { |
| 34 |
refreshing = false; |
| 35 |
}, 250); |
| 36 |
}; |
| 37 |
|
| 38 |
if ($ && $.fn) { |
| 39 |
$(document.body).on('updated_wc_div updated_cart_totals', refreshMiniCart); |
| 40 |
} else { |
| 41 |
document.body.addEventListener('updated_wc_div', refreshMiniCart); |
| 42 |
document.body.addEventListener('updated_cart_totals', refreshMiniCart); |
| 43 |
} |
| 44 |
})(window.jQuery); |
| 45 |
|