WC_Data::get_meta()publicWC 2.6.0

Get Meta Data by Key.

Метод класса: WC_Data{}

Хуки из метода

Возвращает

Разное.

Использование

$WC_Data = new WC_Data();
$WC_Data->get_meta( $key, $single, $context );
$key(строка)
Meta Key.
По умолчанию: ''
$single(true|false)
return first found meta with key, or all with $key.
По умолчанию: true
$context(строка)
What the value is for. Valid values are view and edit.
По умолчанию: 'view'

Список изменений

С версии 2.6.0 Введена.

Код WC_Data::get_meta() WC 8.7.0

public function get_meta( $key = '', $single = true, $context = 'view' ) {
	if ( $this->is_internal_meta_key( $key ) ) {
		$function = 'get_' . ltrim( $key, '_' );

		if ( is_callable( array( $this, $function ) ) ) {
			return $this->{$function}();
		}
	}

	$this->maybe_read_meta_data();
	$meta_data  = $this->get_meta_data();
	$array_keys = array_keys( wp_list_pluck( $meta_data, 'key' ), $key, true );
	$value      = $single ? '' : array();

	if ( ! empty( $array_keys ) ) {
		// We don't use the $this->meta_data property directly here because we don't want meta with a null value (i.e. meta which has been deleted via $this->delete_meta_data()).
		if ( $single ) {
			$value = $meta_data[ current( $array_keys ) ]->value;
		} else {
			$value = array_intersect_key( $meta_data, array_flip( $array_keys ) );
		}
	}

	if ( 'view' === $context ) {
		$value = apply_filters( $this->get_hook_prefix() . $key, $value, $this );
	}

	return $value;
}