WC_Product_Variation::set_attributes
Set attributes. Unlike the parent product which uses terms, variations are assigned specific attributes using name-value pairs. Taxonomy attribute values must use term slugs, which can be URL-encoded and differ from the term name.
For example, if a global "Size" attribute has a term named "7½", whose stored slug is "7%c2%bd", configure the variation using that term slug:
$variation->set_attributes(
array( 'pa_size' => '7%c2%bd', )
);
Method of the class: WC_Product_Variation{}
No Hooks.
Returns
null. Nothing (null).
Usage
$WC_Product_Variation = new WC_Product_Variation(); $WC_Product_Variation->set_attributes( $raw_attributes );
- $raw_attributes(array) (required)
- Array of raw attributes.
Changelog
| Since 3.0.0 | Introduced. |
WC_Product_Variation::set_attributes() WC Product Variation::set attributes code WC 11.3.0
public function set_attributes( $raw_attributes ) {
$raw_attributes = (array) $raw_attributes;
$attributes = array();
foreach ( $raw_attributes as $key => $value ) {
// Remove attribute prefix which meta gets stored with.
if ( 0 === strpos( $key, 'attribute_' ) ) {
$key = substr( $key, 10 );
}
$attributes[ $key ] = $value;
}
$this->set_prop( 'attributes', $attributes );
}