tamarazuk
12/29/2014 - 8:44 PM

WooCommerce Address Validation SmartyStreets badge fix

WooCommerce Address Validation SmartyStreets badge fix

<?php

function wc_address_validation_smartystreets_badge_fix() {
	wc_enqueue_js( '
		$( "body" ).on( "wc_address_validation_fields_mapped", function() {
			$.each( [ "billing", "shipping" ], function( index, addressType ) {

				var fields = $( "#" + addressType + "_address_1, #" + addressType + "_address_2, #" + addressType + "_city, #" + addressType + "_state, #" + addressType + "_postcode, #" + addressType + "_country" );

				fields.on( "visibility", function() {

					var $field = $( this );
					var $smartyBadge = $( "a.smarty-addr-" + addressType );

					setInterval( function() {

						if ( $field.is( ":visible" ) && $smartyBadge.is( ":hidden" ) ) {

							// show the verify address button
							$smartyBadge.parent( "div" ).show();

							$( window ).resize(); // trigger the re-position of .smarty-autocomplete
						}

						if ( $field.is( ":hidden" ) && $smartyBadge.is( ":visible" ) ) {

							// hide the verify address button
							$smartyBadge.parent( "div" ).hide();

							$( window ).resize(); // trigger the re-position of .smarty-autocomplete
						}

					}, 300 );

				}).trigger( "visibility" );

			} );
		} );
	' );
}
add_action( 'wp_enqueue_scripts', 'wc_address_validation_smartystreets_badge_fix' );