Automattic\WooCommerce\Blocks\Domain\Services
CheckoutFields::get_all_fields_from_object
Returns an array of all fields values for a given object in a group.
Method of the class: CheckoutFields{}
Hooks from the method
Returns
Array. An array of fields.
Usage
$CheckoutFields = new CheckoutFields(); $CheckoutFields->get_all_fields_from_object( $wc_object, $group, $all );
- $wc_object(WC_Data) (required)
- The object or order to get the fields for.
- $group(string)
- The group to get the fields for (shipping|billing|other).
Default:'other' - $all(true|false)
- Whether to return all fields or only the ones that are still registered.
Default:false
CheckoutFields::get_all_fields_from_object() CheckoutFields::get all fields from object code WC 10.7.0
public function get_all_fields_from_object( WC_Data $wc_object, string $group = 'other', bool $all = false ) {
$meta_data = [];
$group = $this->prepare_group_name( $group );
$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 ( isset( $value ) ) {
$meta_data[ $missing_field ] = $value;
}
}
return $meta_data;
}