LLMS_Form_Handler::maybe_modify_required_address_fields( array $posted_data, string $location, array[] $fields )

Modify LifterLMS Fields to allow some address fields to be conditionally required.


Description Description

Uses available country locale information to remove the "required" attribute for state and zip code fields when a user has chosen a country that doesn’t use states and/or zip codes.


Top ↑

Parameters Parameters

$posted_data

(array) (Required) User submitted form data (passed by reference).

$location

(string) (Required) Form location ID.

$fields

(array[]) (Required) Array of LifterLMS Form Fields (passed by reference).


Top ↑

Return Return

(void)


Top ↑

Source Source

File: includes/forms/class-llms-form-handler.php

	public function maybe_modify_required_address_fields( &$posted_data, $location, &$fields ) {

		// Only proceed if we have a country to review.
		if ( empty( $posted_data['llms_billing_country'] ) ) {
			return;
		}

		$country = $posted_data['llms_billing_country'];
		$info    = llms_get_country_address_info( $country );

		// Fields to chek.
		$check = array(
			'llms_billing_city'  => 'city',
			'llms_billing_state' => 'state',
			'llms_billing_zip'   => 'postcode',
		);

		foreach ( $check as $post_key => $info_key ) {

			$index = LLMS_Forms::instance()->get_field_by( $fields, 'name', $post_key, 'index' );

			// Field exists, no data was posted, and the field is disabled (is `false`) in the address info array.
			if ( false !== $index && empty( $posted_data[ $post_key ] ) && ! $info[ $info_key ] ) {
				$fields[ $index ]['required'] = false;
			}
		}

	}


Top ↑

Changelog Changelog

Changelog
Version Description
5.0.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.