I'm using the Woocommerce REST API to automatically create product variations when a new product is added. The idea behind it, is that for every new product a variation for the paper size of the product needs to be added and the price calculated automatically using a specific formula. The creation of the different variations happens correctly. However the backend does not seem to select the variation correctly from the dropdown and still shows as "each variation" instead of the selected variation.

Product variation overview

When viewing the details of a specific variation i do however see that all individual variations have been created correctly and the specific variation information has been added. Product Variation description

What i want to achieve is having the name of the attrbute as the selected option in the variation, but without any luck. Any advice on what I still need to add to achieve this? The code which handles the creation of the variations is as followed:

$taxonomy = 'pa_papiersoort';
$papers = get_terms( $taxonomy );    
foreach( $papers as $single_paper ) {
        $price = $this->calculate_price( $post->ID, $single_paper->name, 1 );
        $data = [
            'regular_price' => (string)$price,
            'description'   => $single_paper->name,
            'attributes' => [
                    'name'  => 'Papiersoort',
                    'slug'  => 'papiersoort',
                    'option' => $single_paper->name
        $woocommerce->post("products/$post->ID/variations", $data);

1 Answer 1


This took me a while but i've been able to solve this issue through writing in the database directly so i'd share my solution here for others if anyone comes across the same problem.

To get the attributes selected automatically when creating the variations, 2 things need to happen in the database. The variation post title needs to be adjusted in the form of "product-name variation-name". The second part is adding a new postmeta with the key "attribute_pa_taxonomy" and the meta value being the slug of the selected attribute.

Changing these 2 things in the database directly will assign the selected attribute correctly in the backend overview.

$wpdb->query("UPDATE {$wpdb->prefix}posts SET post_title = '$product->name $single_paper' WHERE ID = $id");
$wpdb->query("INSERT INTO {$wpdb->prefix}postmeta(`post_id`, `meta_key`, 
`meta_value`) VALUES ('$id', 'attribute_pa_papiersoort', '$slug')");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.