ey => $value ) { if ( $this->is_customer_field( $key ) ) { $this->persist_field_for_customer( $key, $value, $customer, $group ); } } } } /** * Copies additional fields from a customer to an order. * * @param WC_Order $order The order to sync the fields for. * @param WC_Customer $customer The customer to sync the fields for. */ public function sync_order_additional_fields_with_customer( WC_Order $order, WC_Customer $customer ) { foreach ( $this->groups as $group ) { $customer_additional_fields = $this->get_all_fields_from_object( $customer, $group, true ); // Sync order additional fields with customer additional fields. foreach ( $customer_additional_fields as $key => $value ) { if ( $this->is_field( $key ) ) { $this->persist_field_for_order( $key, $value, $order, $group, false ); } } } } /** * From a set of fields, returns only the ones for a given location. * * @param array $fields The fields to filter. * @param string $location The location to validate the field for (address|contact|order). * @return array The filtered fields. */ public function filter_fields_for_location( array $fields, string $location ) { if ( 'additional' === $location ) { wc_deprecated_argument( 'location', '8.9.0', 'The "additional" location is deprecated. Use "order" instead.' ); $location = 'order'; } return array_filter( $fields, function ( $key ) use ( $location ) { return $this->is_field( $key ) && $this->get_field_location( $key ) === $location; }, ARRAY_FILTER_USE_KEY ); } /** * Filter fields for order confirmation. * * @param array $fields The fields to filter. * @return array The filtered fields. */ public function filter_fields_for_order_confirmation( $fields ) { return array_filter( $fields, function ( $field ) { return ! empty( $field['show_in_order_confirmation'] ); } ); } /** * Get additional fields for an order. * * @param WC_Order $order Order object. * @param string $location The location to get fields for (address|contact|order). * @param string $group The group to get the field value for (shipping|billing|other). * @param string $context The context to get the field value for (edit|view). * @return array An array of fields definitions as well as their values formatted for display. */ public function get_order_additional_fields_with_values( WC_Order $order, string $location, string $group = 'other', string $context = 'edit' ) { // Because the Additional Checkout Fields API only applies to orders created with Store API, we should not // return any values unless it was created using Store API. This is mainly to prevent "empty" checkbox values // from being shown on the order confirmation page for orders placed using the shortcode. It's rare that this // will happen but not impossible. if ( 'store-api' !== $order->get_created_via() ) { return []; } if ( 'additional' === $location ) { wc_deprecated_argument( 'location', '8.9.0', 'The "additional" location is deprecated. Use "order" instead.' ); $location = 'order'; } if ( 'additional' === $group ) { wc_deprecated_argument( 'group', '8.9.0', 'The "additional" group is deprecated. Use "other" instead.' ); $group = 'other'; } $fields = $this->get_fields_for_location( $location ); $fields_with_values = []; foreach ( $fields as $field_key => $field ) { $value = $this->get_field_from_object( $field_key, $order, $group ); if ( '' === $value || null === $value ) { continue; } if ( 'view' === $context ) { $value = $this->format_additional_field_value( $value, $field ); } $field['value'] = $value; $fields_with_values[ $field_key ] = $field; } return $fields_with_values; } /** * Formats a raw field value for display based on its type definition. * * @param string $value Value to format. * @param array $field Additional field definition. * @return string */ public function format_additional_field_value( $value, $field ) { if ( 'checkbox' === $field['type'] ) { $value = $value ? __( 'Yes', 'woocommerce' ) : __( 'No', 'woocommerce' ); } if ( 'select' === $field['type'] ) { $options = array_column( $field['options'], 'label', 'value' ); $value = isset( $options[ $value ] ) ? $options[ $value ] : $value; } return $value; } /** * Returns a group meta prefix based on its name. * * @param string $group_name The group name (billing|shipping|other). * @return string The group meta prefix. */ public static function get_group_key( $group_name ) { if ( 'additional' === $group_name ) { wc_deprecated_argument( 'group_name', '8.9.0', 'The "additional" group is deprecated. Use "other" instead.' ); $group_name = 'other'; } if ( 'billing' === $group_name ) { return self::BILLING_FIELDS_PREFIX; } if ( 'shipping' === $group_name ) { return self::SHIPPING_FIELDS_PREFIX; } return self::OTHER_FIELDS_PREFIX; } /** * Returns a group name based on passed group key. * * @param string $group_key The group name (_wc_billing|_wc_shipping|_wc_other). * @return string The group meta prefix. */ public static function get_group_name( $group_key ) { if ( '_wc_additional' === $group_key ) { wc_deprecated_argument( 'group_key', '8.9.0', 'The "_wc_additional" group key is deprecated. Use "_wc_other" instead.' ); $group_key = '_wc_other'; } if ( 0 === \strpos( self::BILLING_FIELDS_PREFIX, $group_key ) ) { return 'billing'; } if ( 0 === \strpos( self::SHIPPING_FIELDS_PREFIX, $group_key ) ) { return 'shipping'; } return 'other'; } }