Last active
January 7, 2026 09:17
-
-
Save xlplugins/f23af0279c44a51c4c3c5bf7bb834bf1 to your computer and use it in GitHub Desktop.
Show address fields when specific payment method is selected
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
| class TEMP_WFACP_Conditional_Field_Depend_On_Payment { | |
| private $conditional_field = []; | |
| private $exclude_wfacp_ids = []; | |
| /** | |
| * @var string Payment method bank transfer selected then show fields | |
| */ | |
| private $payment_method_selected = 'fkwcs_stripe_klarna'; | |
| public function __construct() { | |
| $this->conditional_field = [ | |
| 'payment_method' => [ | |
| [ | |
| 'value' => $this->payment_method_selected, | |
| 'fields' => [ 'billing_address_1', 'billing_address_2', 'billing_state', 'billing_city', 'billing_postcode', 'billing_country' ], | |
| 'enable' => true, | |
| ] | |
| ] | |
| ]; | |
| add_action( 'wfacp_after_checkout_page_found', [ $this, 'wfacp_add_script' ] ); | |
| add_filter( 'woocommerce_checkout_fields', [ $this, 'wfacp_remove_required' ], 9, 1 ); | |
| } | |
| function wfacp_add_script() { | |
| if ( in_array( WFACP_Common::get_id(), $this->exclude_wfacp_ids ) ) { | |
| return; | |
| } | |
| add_action( 'wp_footer', [ $this, 'wfacp_conditional_field_script' ] ); | |
| } | |
| function wfacp_conditional_field_script() { | |
| $fields = json_encode( $this->conditional_field ); | |
| ?> | |
| <style> | |
| p.conditional_field { | |
| display: none !important; | |
| } | |
| </style> | |
| <script> | |
| jQuery(document).ready(function ($) { | |
| var conditional_fields = <?php echo $fields; ?>; | |
| conditionField(); | |
| function conditionField() { | |
| $.each(conditional_fields, function (field, values) { | |
| $.each(values, function (key, value) { | |
| var payment_method = $("input[name=payment_method]:checked").val(); | |
| var field = 'payment_method_' + payment_method; | |
| displayConditionalField(field, value, payment_method); | |
| $(document.body).on('change', '.wc_payment_method .input-radio', function () { | |
| var id = $(this).attr('id'); | |
| var val = $(this).val(); | |
| displayConditionalField(id, value, val); | |
| }); | |
| }); | |
| }); | |
| } | |
| function displayConditionalField(id, values, val) { | |
| $.each(values.fields, function (index, field) { | |
| var show = false; | |
| if (val === 'null') { | |
| val = $('#' + id).val(); | |
| } | |
| if (val === values.value) { | |
| show = true; | |
| } | |
| if (show && values.enable) { | |
| $('#' + field + '_field').removeClass('conditional_field'); | |
| } else { | |
| $('#' + field + '_field').addClass('conditional_field'); | |
| } | |
| }); | |
| } | |
| }); | |
| </script> | |
| <?php | |
| } | |
| function wfacp_remove_required( $fields ) { | |
| if ( ! class_exists( 'WFACP_Common' ) || in_array( WFACP_Common::get_id(), $this->exclude_wfacp_ids ) ) { | |
| return $fields; | |
| } | |
| // If payment method is NOT bacs, remove required from these shipping fields | |
| if ( isset( $_POST['payment_method'] ) && $this->payment_method_selected !== $_POST['payment_method'] ) { | |
| $shipping_fields_to_modify = [ 'shipping_address_1', 'shipping_address_2', 'shipping_city', 'shipping_country' ]; | |
| foreach ( $shipping_fields_to_modify as $field_id ) { | |
| if ( isset( $fields['shipping'][ $field_id ] ) ) { | |
| unset( $fields['shipping'][ $field_id ]['required'] ); | |
| } | |
| } | |
| } | |
| return $fields; | |
| } | |
| } | |
| new TEMP_WFACP_Conditional_Field_Depend_On_Payment(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment