Puddinq.com sharing knowledge
';

WooCommerce add attribute to multiple products (programmatically)

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 visualize the products I was going to update with attributes before uncommenting the part in the for each 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;

    // The script looks for these words in the title
    $original_colors = [
        'beige',
        'black',
        'blue',
        'dark blue',
    ];

    // We want to use other words for attributes than used in the title
    $new__colors = [
        'beige' => 'Brown',
        'brown' => 'Brown',
        'blue' => 'Blue',
        'dark blue' => 'Dark blue',
    ];

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

        // if the product does not have a color set
        if (empty($pa_color)) {
            // find colors in the title
            $colors = preg_match("/(" . implode($original_colors, "|") . ")/i", $title, $matches);
            $colors = array_unique($matches);

            // for each found color
            foreach ($colors as $color) {
                $color = strtolower($color);
                // create a taxonomy term with the new color name
                $term_taxonomy_ids = wp_set_object_terms($product->get_id(), $new__colors[$color], 'pa_kleur', true);

                // add the taxonomy term to the product
                $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 the result
            $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;
}