'WooCommerce to update newest group of attributes

I need to programmatically update a product attributes with the current value and found a very useful answer to do just that. However, it seems that in order for the attributes to be added, the terms must be defined first. So I added a wp_insert_term function to add new the terms before updating the attributes. The code works great but it keep appending the new attributes whereas it should only have newest value only. Please advice me if you have a solution.

$product_id = 11874;

$attributes_data = array(
    array('name'=>'Size',  'options'=>array('S', 'L', 'XL', 'XXL'), 'visible' => 1, 'variation' => 1 ),
    array('name'=>'Color', 'options'=>array('Red', 'Blue', 'Black', 'White'), 'visible' => 1, 'variation' => 1 )
);

if( sizeof($attributes_data) > 0 ){
    $attributes = array(); // Initializing

    // Loop through defined attribute data
    foreach( $attributes_data as $key => $attribute_array ) {
        if( isset($attribute_array['name']) && isset($attribute_array['options']) ){
            // Clean attribute name to get the taxonomy
            $taxonomy = 'pa_' . wc_sanitize_taxonomy_name( $attribute_array['name'] );

            $option_term_ids = array(); // Initializing

            // Loop through defined attribute data options (terms values)
            foreach( $attribute_array['options'] as $option ){
                if( !term_exists( $option, $taxonomy ) ){
                   wp_insert_term($option, $taxonomy);
                 }
                 // Save the possible option value for the attribute which will be used for variation later
                wp_set_object_terms( $product_id, $option, $taxonomy, true );
                // Get the term ID
                $option_term_ids[] = get_term_by( 'name', $option, $taxonomy )->term_id;
            
            }
        }
        // Loop through defined attribute data
        $attributes[$taxonomy] = array(
            'name'          => $taxonomy,
            'value'         => $option_term_ids, // Need to be term IDs
            'position'      => $key + 1,
            'is_visible'    => $attribute_array['visible'],
            'is_variation'  => $attribute_array['variation'],
            'is_taxonomy'   => '1'
        );
    }
    // Save the meta entry for product attributes
    update_post_meta( $product_id, '_product_attributes', $attributes );
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source