Save custom attributes of product in woocommerce

I have some custom attributes for each product in woocommerce, I can edit them at the Attributes tab. I can also read them using this code

$product = new WC_Product($product_id);
$v = $product-get_attribute('myattr_name');

But I don't understand how to set this attribute value. Googling not helps. Do you know? I am looking for something like this

$product-set_attribute('myattr_name', $new_value);

But as I can see, there is no such method.

Topic woocommerce-offtopic customization plugins Wordpress

Category Web


Finally I got it working with next code:

$product = wc_get_product($productId);
$attrName = 'my_attribute';
$attrValue = 'some value'

$attr = new WC_Product_Attribute();
$attr->set_name( $attrName );
$attr->set_options( [$attrValue] );
$attr->set_visible(1);
$attr->set_variation(1);
$attr->set_position(0);
$attr->set_id( 0 );
$attrs[$attrName] = $attr;
$product->set_attributes($attrs);
$product->save();

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.