WC_Data::set_props
Set a collection of props in one go, collect any errors, and return the result. Only sets using public methods.
Method of the class: WC_Data{}
No Hooks.
Returns
true|false|WP_Error.
Usage
$WC_Data = new WC_Data(); $WC_Data->set_props( $props, $context );
- $props(array) (required)
- Key value pairs to set. Key is the prop and should map to a setter function name.
- $context(string)
- In what context to run this.
Default:'set'
Changelog
| Since 3.0.0 | Introduced. |
WC_Data::set_props() WC Data::set props code WC 10.8.1
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;
}