Created
May 6, 2026 12:34
-
-
Save xlplugins/95ff3b310ee701408ae9e465a21a7aae to your computer and use it in GitHub Desktop.
Saved Addresses Trigger Fk checkout Field Change Events
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Plugin Name: Saved Addresses → Trigger Field Change Events | |
| * Description: The "Saved Addresses for WooCommerce" plugin auto-fills checkout | |
| * address fields via jQuery .val() but never triggers input/change | |
| * events. That breaks floating-label libraries (labels stay in | |
| * their empty-state position over the filled value), WC's | |
| * update_checkout listeners, and any third-party validators bound | |
| * to those fields. This snippet listens for the plugin's two AJAX | |
| * actions and dispatches input/change/keyup/blur on every populated | |
| * billing & shipping field after the response handler finishes. | |
| * Author: Custom snippet | |
| * Version: 1.0.0 | |
| */ | |
| defined( 'ABSPATH' ) || exit; | |
| add_action( 'wp_footer', function () { | |
| if ( ! function_exists( 'is_checkout' ) ) { | |
| return; | |
| } | |
| if ( ! is_checkout() && ! is_account_page() ) { | |
| return; | |
| } | |
| ?> | |
| <script> | |
| (function ($) { | |
| if (!$ || !$.fn || !$.fn.ajaxComplete) { | |
| return; | |
| } | |
| var FIELDS = [ | |
| 'first_name', 'last_name', 'company', | |
| 'address_1', 'address_2', 'city', | |
| 'postcode', 'state', 'phone', 'email' | |
| ]; | |
| function fireFieldEvents(prefix) { | |
| $.each(FIELDS, function (_, key) { | |
| var $el = $('#' + prefix + '_' + key); | |
| if (!$el.length) { | |
| return; | |
| } | |
| // input → floating-label libs and modern listeners | |
| // change → WC update_checkout, validators | |
| // keyup/blur → legacy validators that bind to those | |
| $el.trigger('input').trigger('change').trigger('keyup').trigger('blur'); | |
| }); | |
| } | |
| $(document).ajaxComplete(function (event, xhr, settings) { | |
| var body = ''; | |
| if (typeof settings.data === 'string') { | |
| body = settings.data; | |
| } else if (settings.data && typeof settings.data.toString === 'function') { | |
| body = settings.data.toString(); | |
| } | |
| if (body.indexOf('action=select_billing_address') === -1 | |
| && body.indexOf('action=select_shipping_address') === -1) { | |
| return; | |
| } | |
| var prefix = body.indexOf('action=select_shipping_address') !== -1 | |
| ? 'shipping' | |
| : 'billing'; | |
| // Defer past the plugin's own success handler so the values are in | |
| // place before we dispatch events. | |
| setTimeout(function () { | |
| fireFieldEvents(prefix); | |
| $(document.body).trigger('update_checkout'); | |
| }, 50); | |
| }); | |
| })(window.jQuery); | |
| </script> | |
| <?php | |
| }, 99 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment