Automattic\WooCommerce\StoreApi\Utilities
AgenticCheckoutUtils::set_billing_address
Set billing address on customer.
Method of the class: AgenticCheckoutUtils{}
No Hooks.
Returns
null. Nothing (null).
Usage
$result = AgenticCheckoutUtils::set_billing_address( $address, $customer );
- $address(array) (required)
- Address data.
- $customer(WC_Customer) (required)
- Customer instance.
AgenticCheckoutUtils::set_billing_address() AgenticCheckoutUtils::set billing address code WC 10.4.3
public static function set_billing_address( $address, $customer ) {
// Only parse and set name if provided and non-empty.
if ( ! empty( $address['name'] ) ) {
$name = wc_clean( wp_unslash( $address['name'] ) );
$name_parts = explode( ' ', $name, 2 );
$first_name = $name_parts[0];
$last_name = isset( $name_parts[1] ) ? $name_parts[1] : '';
// Set billing names.
$customer->set_billing_first_name( $first_name );
$customer->set_billing_last_name( $last_name );
}
// Sanitize all address fields.
$line_one = wc_clean( wp_unslash( $address['line_one'] ?? '' ) );
$line_two = wc_clean( wp_unslash( $address['line_two'] ?? '' ) );
$city = wc_clean( wp_unslash( $address['city'] ?? '' ) );
$state = wc_clean( wp_unslash( $address['state'] ?? '' ) );
$postal_code = wc_clean( wp_unslash( $address['postal_code'] ?? '' ) );
$country = wc_clean( wp_unslash( $address['country'] ?? '' ) );
// Set billing address fields.
$customer->set_billing_address_1( $line_one );
$customer->set_billing_address_2( $line_two );
$customer->set_billing_city( $city );
$customer->set_billing_state( $state );
$customer->set_billing_postcode( $postal_code );
$customer->set_billing_country( $country );
$customer->save();
}