WC_Data::set_props( array $props, string $context = 'set' )

Set a collection of props in one go, collect any errors, and return the result.


Description Description

Only sets using public methods.


Parameters Parameters

$props

(Required) Key value pairs to set. Key is the prop and should map to a setter function name.

$context

(Optional) In what context to run this.

Default value: 'set'


Top ↑

Return Return

(bool|WP_Error)


Top ↑

Source Source

File: includes/abstracts/abstract-wc-data.php

	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() );
			}
		}

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

Top ↑

Changelog Changelog

Changelog
Version Description
3.0.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.