WC_Data::set_props()publicWC 3.0.0

Set a collection of props in one go, collect any errors, and return the result. Only sets using public methods.

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

Хуков нет.

Возвращает

true|false|WP_Error.

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

$WC_Data = new WC_Data();
$WC_Data->set_props( $props, $context );
$props(массив) (обязательный)
Key value pairs to set. Key is the prop and should map to a setter function name.
$context(строка)
In what context to run this.
По умолчанию: 'set'

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

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

Код WC_Data::set_props() WC 8.7.0

public function set_props( $props, $context = 'set' ) {
	$errors = false;

	foreach ( $props as $prop => $value ) {
		try {
			/**
			 * Checks if the prop being set is allowed, and the value is not null.
			 */
			if ( is_null( $value ) || in_array( $prop, array( 'prop', 'date_prop', 'meta_data' ), true ) ) {
				continue;
			}
			$setter = "set_$prop";

			if ( is_callable( array( $this, $setter ) ) ) {
				$this->{$setter}( $value );
			}
		} catch ( WC_Data_Exception $e ) {
			if ( ! $errors ) {
				$errors = new WP_Error();
			}
			$errors->add( $e->getErrorCode(), $e->getMessage(), array( 'property_name' => $prop ) );
		}
	}

	return $errors && count( $errors->get_error_codes() ) ? $errors : true;
}