How do I make it a variable product and add variations? Are variations handled like attributes?

this code creates a product and adds an attribute(XL size) but i cannot make that attribute used as a variation, width a custom price(etc).

this is a function called via ajax

function createnewproduct(){
$new_post = array(
    'post_title' => "Custom Variable",
    'post_content' => 'Lorem ipsum dolor sit amet...',
    'post_status' => 'publish',
    'post_type' => 'product'

$skuu = randomsku('csm','custom',6);
$post_id = wp_insert_post($new_post);
update_post_meta($post_id, '_sku', $skuu );
update_post_meta( $post_id, '_price', "25" );

//made it variable but variations wont be added!
wp_set_object_terms ($post_id, 'variable', 'product_type');
wp_set_object_terms( $post_id, 'XL', 'pa_size' );
//everything works well but
//how do i make the "pa_size" attribure a variation?

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


this function declares in woocommerce that the new product added is a variable product and adds a "size" attribute . The problem is how we tell woocommerce that "size" attribute is a variation.

  • Sorry, I should have linked to this in my first comment (deleted). :::: Have you tried wp_set_object_terms outside of the Ajax call? What about the $append argument? :::: In a product that already has a variation, how does this info is stored in the database? (And this last question is just an idea of troubleshooting. At least, that what I'd also try...)
    – brasofilo
    Dec 5 '12 at 11:32
  • 1st: Please update your answer instead of leaving comments. 2nd: Use upper-/lowercase letters. At least this is no chat, but Q/A that should be an easy to read reference for later users as well. 3rd: Why is the problem not AJAX? 4th: If it's not AJAX and no one has understood your question so far, you might consider reworking the question.
    – kaiser
    Dec 5 '12 at 11:41

Found the solution to make a product attribute, a variation. Lets say we have:

wp_set_object_terms( $post_id, 'XL', 'pa_size' );

Above is a custom attribute (a size attribute). In order to make it a variation you need to do this:

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

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