Yes, I am assigning global attributes to the product, here is my code.
$attributes = wc_get_attribute_taxonomies();
foreach($product_data['atrebuts'] as $key=>$val){
foreach($attributes as $value){
if($value->attribute_label == $key){
if($val[0]){ wp_set_object_terms( $post_id, $val[0], 'pa_'.$value->attribute_name);
$slug = $value->attribute_name;
$terms = get_terms( array('taxonomy' => 'pa_' . $slug,'hide_empty' => false,));
foreach($terms as $term_id){
if ($term_id->slug==sanitize_title($val[0])){$term_id_id=$term_id->term_id;$term_tax_id=$term_id->term_id;}
}
$new_attr = new WC_Product_Attribute();
$new_attr->set_id( $term_id_id );
$new_attr->set_name('pa_'.$slug);
$new_attr->set_options([$term_tax_id]);
$new_attr->set_visible( true );
$parent_attributes['pa_' . $slug] = $new_attr;
$product->set_attributes( array_values($parent_attributes) );
$arts['pa_'.$slug] = array(
'name' => 'pa_'.$slug,
'value' =>[$term_tax_id],
'postion' => '0',
'is_visible' => '1',
'is_variation' => '0',
'is_taxonomy' => '1'
);
}
}
}
}
$product->save();
update_post_meta( $post_id, '_product_attributes', $arts);
But there is a nuance here, when determining the term_id, I cannot correctly determine this ID for all parameters.
If you check by name, then there is a problem with a dot and a comma, you get a different name for the same values. You can try to bring it to one type, but the slug has exactly a unique meaning and you don’t need to connect various functions, and here I can’t understand why the slug began to turn out as an ID, and not sanitize_title($name)