Programatticaly create a WooCommerce product variation with variations

Stil a work in progress, but I found it hard to get good examples. If you are stuck in creating your script good chance my script gives you hints.

first you create a product if it does not exist. In the loop where I am using it I have the $plugin->name available for the product title.

        /**
         * create product if it does not exist
         */

        $product = get_page_by_title( $plugin->name, OBJECT, 'product' );

        if (is_null($product)) {

            $post = array(
                'post_author' => 2,
                'post_content' => '',
                'post_status' => "publish",
                'post_title' => $plugin->name,
                'post_parent' => '',
                'post_type' => "product",
            );

            $product_id = wp_insert_post( $post, false );

            wp_set_object_terms($product_id, 'variable', 'product_type');

            $product = wc_get_product( $product_id );

        } else {
		
            $product = wc_get_product($product->ID);
			
        }

The above sets $product to the product both when it is new and when it exists.
Now we need to check if a variation exists with the attribute (versie) value which
I have available in $plugin->version

        /**
         * check if variation exists
         */

        $variations = $product->get_available_variations();

        $not_found = true;

        foreach ($variations as $variation) {

            if (sanitize_title($plugin->version) === $variation['attributes']['attribute_pa_versie']) {
              $not_found = false;
            }
        }

        if ($not_found) {
            echo "Variation does not exist {$plugin->version}\n";
            $term_taxonomy_ids = wp_set_object_terms( $product->get_id(), $plugin->version, 'pa_versie', true );

            $term = get_term_by('id', $term_taxonomy_ids[0], 'pa_versie', OBJECT);
            $slug = sanitize_title($plugin->version);
            $thedata = Array(
                'pa_versie'=>Array(
                    'name'=>'pa_versie',
                    'value'=>$slug,
                    'is_visible' => '1',
                    'is_variation' => '1',
                    'is_taxonomy' => '1'
                )
            );
            update_post_meta( $product->get_id(),'_product_attributes', $thedata);

            $post_slug = sanitize_title($product->get_title() . ' ' . $plugin->version);


            $variation_post = array(
                'post_title'  => $plugin->name . ' - ' . $plugin->version,
                'post_name'   => $post_slug,
                'post_status' => 'publish',
                'post_excerpt'=> 'Versie: ' . $plugin->version,
                'post_parent' => $product->get_id(),
                'post_type'   => 'product_variation',
                'guid'        => $product->get_permalink(),
                'meta_input'  => array(
                    'attribute_pa_versie' => $slug
                )
            );

            // Creating the product variation
            $variation_id = wp_insert_post( $variation_post );

            $variation = new WC_Product_Variation( $variation_id );


            ## Set/save all other data

            // SKU
            if( ! empty( $post_slug ) )
                $variation->set_sku( $post_slug );

            // Prices
            $variation->set_price( 10 );
            $variation->set_regular_price( 10 );

            $variation->set_downloadable(true);

            // Stock
            $variation->set_manage_stock(false);
            $variation->set_weight(''); // weight (reseting)
            $variation->save(); // Save the data
        } else {
            echo "Variation exists - {$plugin->version}\n";
        }

The things you have to replace are ‘versie’, ‘Versie’, $plugin->name and $plugin->version.
These are custom for the ettribute I want to fill