WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
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
up vote 2 down vote accepted

Found the solution to make a product attribute, a variation. Lets say we have wp_set_object_terms( $post_id, 'XL', 'pa_size' ); The above is a custom attribute (a size attribute). Making it a variation will look like

$thedata = Array('pa_size'=>Array(
        'is_visible' => '1', 
        'is_variation' => '1',
        'is_taxonomy' => '1'
    update_post_meta( $post_id,'_product_attributes',$thedata);
share|improve this answer

protected by toscho Sep 26 '15 at 8:36

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

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