I have a front-end form I am creating to allow users to publish a variable product to my shop from the front end with predefined attributes and variations.

I have found this very helpful question: here Which shows me how to set the product type as variable and assign my predefined attributes in the attributes section of the product data.

However when I am on the backend of Wordpress/Woocommerce and editing the product I click on variations and none are set, I look at the attributes and my "resolution" attribute is set with my 3 items.

How do I make this to where it will actually set those attributes to variations of my form? Do I need to use wp_insert_post? Looking in phpmyadmin it just looks like product variations are assigned to a parent_id (product id) and post type is product_varition and so on.

$new_post = array(
'post_title' => esc_attr(strip_tags($_POST['postTitle'])),
'post_content' => esc_attr(strip_tags($_POST['postContent'])),
'post_status' => 'publish',
'post_type' => 'product',
'tags_input' => array($tags)

$skuu = rand();
$post_id = wp_insert_post($new_post);
update_post_meta($post_id, '_sku', $skuu );

//my array for setting the attributes
$avail_attributes = array(

//Sets the attributes up to be used as variations but doesnt actually set them up as variations
wp_set_object_terms ($post_id, 'variable', 'product_type');
wp_set_object_terms( $post_id, $avail_attributes, 'pa_resolution' );

$thedata = array(
'pa_resolution'=> array(
                'is_visible' => '1',
                'is_variation' => '1',
                'is_taxonomy' => '1'
update_post_meta( $post_id,'_product_attributes',$thedata);

update_post_meta( $post_id, '_visibility', 'search' );
update_post_meta( $post_id, '_stock_status', 'instock');

So just to be clear (I tend to be confusing) the above does create my variable product from the front end, and when I look at the product in the backend it is a variable product, it has the resolution attribute set and has my 3 terms (high-res, medium-res, low-res) as attributes. I just need to take this a step further where they are actually set as variations so people can place an order.

Adds attributes to product already need it to add variations as well


I got it working for my situation by using update_post_meta and wp_insert_post. Because I already setup my attributes and terms all I needed was a way to add to the above code so that when the product is created it not only will assign the attributes to the product but insert them as variations in the database.

Here is my solution:

//insert variations post_type
while ($i<=3) {
$my_post = array(
      'post_title'    => 'Variation #' . $i . ' of ' . esc_attr(strip_tags($_POST['postTitle'])),
      'post_name'     => 'product-' . $post_id . '-variation-' . $i,
      'post_status'   => 'publish',
      'post_parent'   => $post_id,
      'post_type'     => 'product_variation',
      'guid'          =>  home_url() . '/?product_variation=product-' . $post_id . '-variation-' . $i

    // Insert the post into the database
    wp_insert_post( $my_post );

    $variable_id = $post_id + 1;
    $variable_two = $variable_id + 1;
    $variable_three = $variable_two + 1;

    update_post_meta( $variable_id, 'attribute_pa_resolution', 'high-resolution');
    update_post_meta( $variable_id, '_price', 8.50 );
    update_post_meta( $variable_id, '_regular_price', '8.50');

    update_post_meta( $variable_two, 'attribute_pa_resolution', 'medium-resolution');
    update_post_meta( $variable_two, '_price', 5.50 );
    update_post_meta( $variable_two, '_regular_price', '5.50');

    update_post_meta( $variable_three, 'attribute_pa_resolution', 'low-resolution');
    update_post_meta( $variable_three, '_price', 3.50 );
    update_post_meta( $variable_three, '_regular_price', '3.50');


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