Automattic\WooCommerce\Blocks\Domain\Services
CheckoutFieldsFrontend::save_address_fields()
For the My Account page, save address fields. This uses the Store API endpoint for saving addresses so extensibility hooks are consistent across the codebase.
The caller saves the customer object if there are no errors. Nonces are checked before this method executes.
Method of the class: CheckoutFieldsFrontend{}
No Hooks.
Return
null
. Nothing (null).
Usage
$CheckoutFieldsFrontend = new CheckoutFieldsFrontend(); $CheckoutFieldsFrontend->save_address_fields( $user_id, $address_type, $address, $customer );
- $user_id(int) (required)
- User ID.
- $address_type(string) (required)
- Type of address (billing or shipping).
- $address(array) (required)
- Address fields.
- $customer(WC_Customer) (required)
- Customer object.
CheckoutFieldsFrontend::save_address_fields() CheckoutFieldsFrontend::save address fields code WC 9.2.3
public function save_address_fields( $user_id, $address_type, $address, $customer ) { // phpcs:disable WordPress.Security.NonceVerification.Missing $additional_fields = $this->checkout_fields_controller->get_fields_for_location( 'address' ); $field_values = array(); foreach ( array_keys( $additional_fields ) as $key ) { $post_key = CheckoutFields::get_group_key( $address_type ) . $key; if ( ! isset( $_POST[ $post_key ] ) ) { continue; } $field_value = $this->checkout_fields_controller->sanitize_field( $key, wc_clean( wp_unslash( $_POST[ $post_key ] ) ) ); $validation = $this->checkout_fields_controller->validate_field( $key, $field_value ); if ( is_wp_error( $validation ) && $validation->has_errors() ) { wc_add_notice( $validation->get_error_message(), 'error' ); continue; } $field_values[ $key ] = $field_value; } // Persist individual additional fields to customer. foreach ( $field_values as $key => $value ) { $this->checkout_fields_controller->persist_field_for_customer( $key, $value, $customer, $address_type ); } // Validate all fields for this location. $location_validation = $this->checkout_fields_controller->validate_fields_for_location( array_merge( $address, $field_values ), 'address', $address_type ); if ( is_wp_error( $location_validation ) && $location_validation->has_errors() ) { wc_add_notice( $location_validation->get_error_message(), 'error' ); } // phpcs:enable WordPress.Security.NonceVerification.Missing }