WooCommerce add attribute to multiple products (programmatically)

This snippets was used in winkel-centrum.nl, I needed to adjust an import and add colors found in the title as attributes. I had imported the products and wanted to extract a word from the title to add as attribute in the product. I used a shortcode because I wanted to visualisy the products I was going to update with attributes before uncommeting the part in the foreach loop where I update the attribute. This script will not work out of the box for you (attributes are dutch colors) but it can help you solving your specific wish (maybe).

add_shortcode('winkel', 'winkel_show_color');

function winkel_show_color()
{

    $products = wc_get_products(
        array(
            'tag' => array('Wannahaves'),
            'limit' => 1000,
        )
    );
    $output = '<table>';
    $e = 0;

    $original_colors = [
        'beige',
        'bruin',
        'blauw',
        'donkerblauw',
        'fuchsia',
        'grijs',
        'groen',
        'koraal',
        'mintgroen',
        'oranje',
        'rood',
        'white',
        'wit',
        'bordeaux',
        'rose',
        'roze',
        'turquoise',
        'zalm',
        'zwart'
    ];
    $new__colors = [
        'beige' => 'Bruin',
        'bruin' => 'Bruin',
        'blauw' => 'Blauw',
        'donkerblauw' => 'Blauw',
        'fuchsia' => 'Paars',
        'grijs' => 'Grijs',
        'groen' => 'Groen',
        'koraal' => 'Rood',
        'mintgroen' => 'Groen',
        'oranje' => 'Oranje',
        'rood' => 'Rood',
        'white' => 'Wit',
        'wit' => 'Wit',
        'bordeaux' => 'Rood',
        'rose' => 'Rose',
        'roze' => 'Roze',
        'turquoise' => 'Paars',
        'zalm' => 'Roze',
        'zwart' => 'Zwart'
    ];

    /** @var WC_Product $product */
    foreach ($products as $product) {
        $pa_color = $product->get_attribute('pa_kleur');
        $title = $product->get_title();

        if (empty($pa_color)) {
            $colors = preg_match("/(" . implode($original_colors, "|") . ")/i", $title, $matches);
            $colors = array_unique($matches);

            foreach ($colors as $color) {
                $color = strtolower($color);
                $term_taxonomy_ids = wp_set_object_terms($product->get_id(), $new__colors[$color], 'pa_kleur', true);

                $thedata = array(
                    'pa_kleur' => array(
                        'name' => 'pa_kleur',
                        'value' => $new__colors[$color],
                        'is_visible' => '1',
                        'is_taxonomy' => '1'
                    )
                );
                update_post_meta($product->get_id(), '_product_attributes', $thedata);
            }

            $output .= '<tr>';
            $output .= '<td>';
            $output .= ++$e;
            $output .= '</td>';
            $output .= '<td>';
            $output .= $title;
            $output .= '</td>';
            $output .= '<td>';
            $output .= implode(', ', $colors);
            $output .= '</td>';
            $output .= '</tr>';
        }
    }

    $output .= '</table>';

    return $output;
}