• 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 (https://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 (https://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: https://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://www.ads-software.com/plugins/woocommerce/

Viewing 2 replies - 1 through 2 (of 2 total)
  • Hi @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

    (@ralgomes)

    Sure thing, here’s the code as I’m running right now: https://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.