Title: Adding product variations via code doesn&#039;t work
Last modified: August 30, 2016

---

# Adding product variations via code doesn't work

 *  [ralgomes](https://wordpress.org/support/users/ralgomes/)
 * (@ralgomes)
 * [10 years, 11 months ago](https://wordpress.org/support/topic/adding-product-variations-via-code-doesnt-work/)
 * I’m setting up a store to sell collectible cards, which requires at least 3 product
   variations: language, type (normal, foil) and condition (mint, etc). I got a 
   price database that references these three variations and to keep my sanity I
   decided to import them programatically.
 * The best code I could get to automatically create the possible variations is 
   this:
 * 1) Create the product variation kinds manually in Products / Attributes ([http://imgur.com/9bBiWtL](http://imgur.com/9bBiWtL))
 * 2) Write a wp plugin, requiring woocommerce to load first, that for each card,
   runs:
 * >  wp_set_object_terms( $id, array( ‘portugues’, ‘ingles’, ‘outro’ ), ‘pa_idioma’);
   > 
   > wp_set_object_terms( $id, array( ‘normal’, ‘foil’, ‘outro’ ), ‘pa_tipo’); wp_set_object_terms(
   > $id, array( ‘m’, ‘nm’, ‘sp’, ‘mp’ ), ‘pa_condicao’); $attributes = array();
   > $attributes[‘pa_idioma’] = array( ‘name’=>’pa_idioma’, ‘value’=> array( ‘portugues’,‘
   > ingles’, ‘outro’ ), ‘position’ => 1, ‘is_visible’ => true, ‘is_variation’ =
   > > true, ‘is_taxonomy’ => true ); $attributes[‘pa_tipo’] = array( ‘name’=>’pa_tipo’,‘
   > value’=> array( ‘normal’, ‘foil’, ‘outro’ ), ‘position’ => 2, ‘is_visible’ 
   > => true, ‘is_variation’ => true, ‘is_taxonomy’ => true ); $attributes[‘pa_condicao’]
   > = array( ‘name’=>’pa_condicao’, ‘value’=> array( ‘m’, ‘nm’, ‘sp’, ‘mp’ ), ‘
   > position’ => 3, ‘is_visible’ => true, ‘is_variation’ => true, ‘is_taxonomy’
   > => true ); update_post_meta( $id, ‘_product_attributes’, $attributes );
 * 3) Finally, for each variation we actually have, run:
 * >  $i++;
   >  $vari_post = array( ‘post_title’=> $meta[‘name_en’][0].’ ‘.$variation[‘
   > idioma’].’ ‘.$variation[‘tipo’].’ ‘.$variation[‘condicao’], ‘post_name’ => 
   > slugify($meta[‘name_en’][0].’ ‘.$variation[‘idioma’].’ ‘.$variation[‘tipo’].’‘.
   > $variation[‘condicao’]), ‘post_status’ => ‘publish’, ‘post_parent’ => $id, ‘
   > post_type’ => ‘product_variation’, ‘guid’=>home_url() . ‘/?product_variation
   > =product-‘ . $id . ‘-variation-‘ . $i ); $vari_post_ID = wp_insert_post( $vari_post);
   >  wp_set_object_terms( $vari_post_ID, $variation[‘idioma’], ‘pa_idioma’, false);
   > 
   > wp_set_object_terms( $vari_post_ID, $variation[‘tipo’], ‘pa_tipo’, false); 
   > wp_set_object_terms( $vari_post_ID, $variation[‘condicao’], ‘pa_condicao’, 
   > false);
   >  $vari_data = get_post_meta( $vari_post_ID, ‘_product_attributes’ );
   >  $vari_data[‘
   > pa_idioma’] = array( ‘name’=>’pa_idioma’, ‘value’=> $variation[‘idioma’], ‘
   > position’ => 1, ‘is_visible’ => true, ‘is_variation’ => true, ‘is_taxonomy’
   > => true ); $vari_data[‘pa_tipo’] = array( ‘name’=>’pa_tipo’, ‘value’=> $variation[‘
   > tipo’], ‘position’ => 2, ‘is_visible’ => true, ‘is_variation’ => true, ‘is_taxonomy’
   > => true ); $vari_data[‘pa_condicao’] = array( ‘name’=>’pa_condicao’, ‘value’
   > => $variation[‘condicao’], ‘position’ => 3, ‘is_visible’ => true, ‘is_variation’
   > => true, ‘is_taxonomy’ => true ); update_post_meta( $vari_post_ID, ‘_product_attributes’,
   > $vari_data );
 * This _partially works_, because I see that the attributes are associated correctly
   at the product’s admin page ([http://imgur.com/GEAono4](http://imgur.com/GEAono4)),
   and in the variations tab I can see one variation created for each subproduct
   we have. Stuff like price and SKU (not shown in the code above) were inserted
   correctly for each one. But the attributes appear unselected, as seen here: [http://imgur.com/FeuEaGy](http://imgur.com/FeuEaGy)
 * I don’t know how to make the attributes for each variation to be setup correctly.
   Any ideas of what’s missing / wrong here?
 * [https://wordpress.org/plugins/woocommerce/](https://wordpress.org/plugins/woocommerce/)

Viewing 2 replies - 1 through 2 (of 2 total)

 *  [Kharis Sulistiyono](https://wordpress.org/support/users/kharisblank/)
 * (@kharisblank)
 * [10 years, 11 months ago](https://wordpress.org/support/topic/adding-product-variations-via-code-doesnt-work/#post-6272957)
 * Hi [@ralgomes](https://wordpress.org/support/users/ralgomes/),
 * To set product price, use this:
 *     ```
       update_post_meta( $post_id, '_regular_price', $product_regular_price );
       ```
   
 * To set product SKU, use this:
 *     ```
       update_post_meta( $post_id, '_sku', $product_sku );
       ```
   
 * >  I don’t know how to make the attributes for each variation to be setup correctly.
   > Any ideas of what’s missing / wrong here?
 * I need to review the full codes. Could you please share the full codes?
 *  Thread Starter [ralgomes](https://wordpress.org/support/users/ralgomes/)
 * (@ralgomes)
 * [10 years, 11 months ago](https://wordpress.org/support/topic/adding-product-variations-via-code-doesnt-work/#post-6273023)
 * Sure thing, here’s the code as I’m running right now: [http://pastebin.com/7CjF28HB](http://pastebin.com/7CjF28HB)
 * You can see the _sku and _regular_price were already being imported. These are
   working correctly.

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Adding product variations via code doesn't work’ is closed to new replies.

 * ![](https://ps.w.org/woocommerce/assets/icon.svg?rev=3234504)
 * [WooCommerce](https://wordpress.org/plugins/woocommerce/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/woocommerce/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/woocommerce/)
 * [Active Topics](https://wordpress.org/support/plugin/woocommerce/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/woocommerce/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/woocommerce/reviews/)

## Tags

 * [variable products](https://wordpress.org/support/topic-tag/variable-products/)

 * 2 replies
 * 2 participants
 * Last reply from: [ralgomes](https://wordpress.org/support/users/ralgomes/)
 * Last activity: [10 years, 11 months ago](https://wordpress.org/support/topic/adding-product-variations-via-code-doesnt-work/#post-6273023)
 * Status: not resolved