I am trying to add a new attribute with multiple values to an existing WooCommerce product.
This is the code I am using. I simplified it a bit to make it more clear.
$productId = 87356;
$attr = get_post_meta( $productId, '_product_attributes', true );
$gears = [ '3', '7' ];
$attr['pa_aantal-versnellingen'] = [
'name' => 'pa_aantal-versnellingen',
'value' => implode(' | ', $gears),
'is_visible' => '1',
'is_variation' => '1',
'is_taxonomy' => '1'
];
update_post_meta( $productId, '_product_attributes', $attr );
foreach ( $gears as $gear ) {
wp_set_object_terms( $productId, $gear, 'pa_aantal-versnellingen', true );
}
The attribute appears in the list of attributes on the product. However the terms are not added.
The terms also do exist in the DB:
What do I do wrong? I did a lot of research, read some other questions but they didn't help me.
Create new product attribute programmatically in Woocommerce