Skip to content

Instantly share code, notes, and snippets.

@xlplugins
Created April 3, 2025 11:20
Show Gist options
  • Save xlplugins/c2ca960132ccdbd14fdbbb729295dc2e to your computer and use it in GitHub Desktop.
Save xlplugins/c2ca960132ccdbd14fdbbb729295dc2e to your computer and use it in GitHub Desktop.
Checkout: postcode field as dropdown
class WFACP_City_FIELD_To_DROPDOWN_FOR_IL {
public function __construct() {
add_filter( 'woocommerce_form_field_args', [ $this, 'field_args' ], 10, 2 );
add_action( 'wfacp_internal_css', [ $this, 'js' ] );
}
public function field_args( $fields, $key ) {
if ( $key == 'shipping_postcode' || $key == 'billing_postcode' ) {
$fields = wp_parse_args( array(
'type' => 'select',
'options' => array(
'' => __( 'Select your postcode' ),
'Caloocan' => 'Caloocan',
'Las Piñas' => 'Las Piñas',
'Makati' => 'Makati',
'Malabon' => 'Malabon',
'Mandaluyong' => 'Mandaluyong',
'Manila' => 'Manila',
'Marikina' => 'Marikina',
'Muntinlupa' => 'Muntinlupa',
'Navotas' => 'Navotas' ,
'Parañaque' => 'Parañaque',
'Pasay' => 'Pasay',
'Pasig' => 'Pasig',
'Pateros (Municipality)' => 'Pateros (Municipality)',
'Quezon City' => 'Quezon City',
'San Juan' => 'San Juan',
'Taguig' => 'Taguig',
'Valenzuela' => 'Valenzuela' ,
),
), $fields );
}
return $fields;
}
public function js() {
?>
<style>
body .wfacp-form .woocommerce form .form-row .select2-container {
width: 100% !important;
}
#wfacp-e-form label.wfacp-form-control-label {
right: 23px;
}
.wfacp-form-control-wrapper .select2-container .select2-selection--single {
margin: 0;
}
</style>
<script>
window.addEventListener('load', function () {
(function ($) {
run_fields();
function run_fields() {
if ($('#shipping_postcode').length > 0) {
run_select2($('#billing_postcode'));
}
if ($('#billing_postcode').length > 0) {
run_select2($('#shipping_postcode'));
}
}
$(document.body).on("change", "#billing_same_as_shipping_field", function (e) {
run_fields();
});
$(document.body).on("change", "#shipping_same_as_billing_field", function (e) {
run_fields();
});
function run_select2(el) {
if (el[0].tagName === 'select' || el[0].tagName === 'SELECT') {
el.select2();
}
}
$(document.body).on('wfacp_step_switching', function (e, v) {
setTimeout(function () {
run_fields();
}, 1000);
});
})(jQuery);
});
</script>
<?php
}
}
new WFACP_City_FIELD_To_DROPDOWN_FOR_IL();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment