ol( $value ) ) { $value = $value ? '1' : '0'; } // Replacing all meta using `add_meta_data`. For some reason `update_meta_data` causes duplicate keys. $wc_object->add_meta_data( $meta_key, $value, true ); } /** * Returns a field value for a given object. * * @param string $key The field key. * @param WC_Customer|WC_Order $wc_object The customer or order to get the field value for. * @param string $group The group to get the field value for (shipping|billing|other). * * @return mixed The field value. */ public function get_field_from_object( string $key, WC_Data $wc_object, string $group = 'other' ) { if ( 'additional' === $group ) { wc_deprecated_argument( 'group', '8.9.0', 'The "additional" group is deprecated. Use "other" instead.' ); $group = 'other'; } $meta_key = self::get_group_key( $group ) . $key; $value = $wc_object->get_meta( $meta_key, true ); if ( ! $value ) { /** * Allow providing a default value for additional fields if no value is already set. * * @param null $value The default value for the filter, always null. * @param string $group The group of this key (shipping|billing|other). * @param WC_Data $wc_object The object to get the field value for. * * @since 8.9.0 */ $value = apply_filters( "woocommerce_get_default_value_for_{$key}", null, $group, $wc_object ); } // We cast the value to a boolean if the field is a checkbox. if ( $this->is_field( $key ) && 'checkbox' === $this->additional_fields[ $key ]['type'] ) { return '1' === $value; } if ( null === $value ) { return ''; } return $value; } /** * Returns an array of all fields values for a given object in a group. * * @param WC_Data $wc_object The object or order to get the fields for. * @param string $group The group to get the fields for (shipping|billing|other). * @param bool $all Whether to return all fields or only the ones that are still registered. Default false. * * @return array An array of fields. */ public function get_all_fields_from_object( WC_Data $wc_object, string $group = 'other', bool $all = false ) { if ( 'additional' === $group ) { wc_deprecated_argument( 'group', '8.9.0', 'The "additional" group is deprecated. Use "other" instead.' ); $group = 'other'; } $meta_data = []; $prefix = self::get_group_key( $group ); if ( $wc_object instanceof WC_Data ) { $meta = $wc_object->get_meta_data(); foreach ( $meta as $meta_data_object ) { if ( 0 === \strpos( $meta_data_object->key, $prefix ) ) { $key = \str_replace( $prefix, '', $meta_data_object->key ); if ( $all || $this->is_field( $key ) ) { $meta_data[ $key ] = $meta_data_object->value; } } } } $missing_fields = array_diff( array_keys( $this->get_fields_for_group( $group ) ), array_keys( $meta_data ) ); foreach ( $missing_fields as $missing_field ) { /** * Allow providing a default value for additional fields if no value is already set. * * @param null $value The default value for the filter, always null. * @param string $group The group of this key (shipping|billing|other). * @param WC_Data $wc_object The object to get the field value for. * * @since 8.9.0 */ $value = apply_filters( "woocommerce_get_default_value_for_{$missing_field}", null, $group, $wc_object ); if ( $value ) { $meta_data[ $missing_field ] = $value; } } return $meta_data; } /** * Copies additional fields from an order to a customer. * * @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_customer_additional_fields_with_order( WC_Order $order, WC_Customer $customer ) { foreach ( $this->groups as $group ) { $order_additional_fields = $this->get_all_fields_from_object( $order, $group, true ); // Sync customer additional fields with order additional fields. foreach ( $order_additional_fields as $key => $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' ) { 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'; } }