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.
Returns
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)
- Address fields.
Default: [] - $customer(WC_Customer)
- Customer object.
Default: null
CheckoutFieldsFrontend::save_address_fields() CheckoutFieldsFrontend::save address fields code WC 9.8.5
public function save_address_fields( $user_id, $address_type, $address = [], $customer = null ) { try { $customer = $customer ?? new WC_Customer( $user_id ); $result = $this->update_additional_fields_for_customer( $customer, 'address', $address_type ); if ( is_wp_error( $result ) ) { foreach ( $result->get_error_messages() as $error_message ) { wc_add_notice( $error_message, 'error' ); } } $customer->save(); } catch ( \Exception $e ) { wc_add_notice( sprintf( /* translators: %s: Error message. */ __( 'An error occurred while saving address details: %s', 'woocommerce' ), esc_html( $e->getMessage() ) ), 'error' ); } }