$request ); $customer->save(); $this->cart_controller->calculate_totals(); return rest_ensure_response( $this->schema->get_item_response( $cart ) ); } /** * Get full customer billing address. * * @param \WC_Customer $customer Customer object. * @return array */ protected function get_customer_billing_address( \WC_Customer $customer ) { $validation_util = new ValidationUtils(); $billing_country = $customer->get_billing_country(); $billing_state = $customer->get_billing_state(); $additional_fields = $this->additional_fields_controller->get_all_fields_from_customer( $customer ); $additional_fields = array_reduce( array_keys( $additional_fields ), function( $carry, $key ) use ( $additional_fields ) { if ( 0 === strpos( $key, '/billing/' ) ) { $value = $additional_fields[ $key ]; $key = str_replace( '/billing/', '', $key ); $carry[ $key ] = $value; } return $carry; }, array() ); /** * There's a bug in WooCommerce core in which not having a state ("") would result in us validating against the store's state. * This resets the state to an empty string if it doesn't match the country. * * @todo Removing this handling once we fix the issue with the state value always being the store one. */ if ( ! $validation_util->validate_state( $billing_state, $billing_country ) ) { $billing_state = ''; } return array_merge( [ 'first_name' => $customer->get_billing_first_name(), 'last_name' => $customer->get_billing_last_name(), 'company' => $customer->get_billing_company(), 'address_1' => $customer->get_billing_address_1(), 'address_2' => $customer->get_billing_address_2(), 'city' => $customer->get_billing_city(), 'state' => $billing_state, 'postcode' => $customer->get_billing_postcode(), 'country' => $billing_country, 'phone' => $customer->get_billing_phone(), 'email' => $customer->get_billing_email(), ], $additional_fields ); } /** * Get full customer shipping address. * * @param \WC_Customer $customer Customer object. * @return array */ protected function get_customer_shipping_address( \WC_Customer $customer ) { $additional_fields = $this->additional_fields_controller->get_all_fields_from_customer( $customer ); $additional_fields = array_reduce( array_keys( $additional_fields ), function( $carry, $key ) use ( $additional_fields ) { if ( 0 === strpos( $key, '/shipping/' ) ) { $value = $additional_fields[ $key ]; $key = str_replace( '/shipping/', '', $key ); $carry[ $key ] = $value; } return $carry; }, array() ); return array_merge( [ 'first_name' => $customer->get_shipping_first_name(), 'last_name' => $customer->get_shipping_last_name(), 'company' => $customer->get_shipping_company(), 'address_1' => $customer->get_shipping_address_1(), 'address_2' => $customer->get_shipping_address_2(), 'city' => $customer->get_shipping_city(), 'state' => $customer->get_shipping_state(), 'postcode' => $customer->get_shipping_postcode(), 'country' => $customer->get_shipping_country(), 'phone' => $customer->get_shipping_phone(), ], $additional_fields ); } }