Support » Plugin: WooCommerce » programmatically make an attribute a variation

Viewing 2 replies - 1 through 2 (of 2 total)
  • hi mark, it seems i’m following your steps on this topic, on stackoverflow and hereā€”did you manage to get this working?

    i’m stuck with the creation of attributes and their terms
    can you shed some light?

    hi yes i did, i posted it in wordpress overflow, i really forgot to post it here also!

    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
    $new_post = array(
    ‘post_title’ => “Custom Variable”,
    ‘post_content’ => ‘Lorem ipsum dolor sit amet…’,
    ‘post_status’ => ‘publish’,
    ‘post_type’ => ‘product’
    );

    $skuu = “random”;
    $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’ );
    /* the solution is here under */
    $thedata = Array(‘pa_size’=>Array(
    ‘name’=>’pa_size’,
    ‘value’=>”,
    ‘is_visible’ => ‘1’,
    ‘is_variation’ => ‘1’,
    ‘is_taxonomy’ => ‘1’
    ));
    update_post_meta( $post_id,’_product_attributes’,$thedata);

    /* end of my solution */

    update_post_meta( $post_id, ‘_visibility’, ‘search’ );
    update_post_meta( $post_id, ‘_stock_status’, ‘instock’);

    so now you have it pa_size its a variation

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘programmatically make an attribute a variation’ is closed to new replies.