utilizo este código:
$sku = 21333;
$size = 'S';
$stock = 2;
$price_a = 60;
$price_b = 30;
$product_parent = get_product_by_sku($sku);
$product = new WC_Product_Variable($product_parent->id);
$variations = $product->get_available_variations();
// First off all delete all variations
foreach($variations as $prod_variation) {
$metaid=mysql_query("SELECT meta_id FROM wp_postmeta WHERE post_id = ".$prod_variation['variation_id']);
while ($row = mysql_fetch_assoc($metaid)) {
mysql_query("DELETE FROM wp_postmeta WHERE meta_id = ".$row['meta_id']);
}
mysql_query("DELETE FROM wp_posts WHERE ID = ".$prod_variation['variation_id']);
}
// Now add new variation
$thevariation = array(
'post_title'=> '',
'post_name' => 'product-' . $product_parent->id . '-variation',
'post_status' => 'publish',
'post_parent' => $product_parent->id,
'post_type' => 'product_variation',
'guid'=>home_url() . '/?product_variation=product-' . $product_parent->id . '-variation'
);
$variation_id = wp_insert_post($thevariation);
update_post_meta($variation_id, 'post_title', 'Variation #' . $variation_id . ' of '. $product_parent->id);
wp_set_object_terms($variation_id, $size, 'pa_size');
update_post_meta($variation_id, 'attribute_pa_size', $size);
update_post_meta($variation_id, '_regular_price', $price_a);
update_post_meta($variation_id, '_downloadable_files', '');
update_post_meta($variation_id, '_download_expiry', '');
update_post_meta($variation_id, '_download_limit', '');
update_post_meta($variation_id, '_sale_price_dates_to', '');
update_post_meta($variation_id, '_sale_price_dates_from', '');
update_post_meta($variation_id, '_backorders', 'no');
update_post_meta($variation_id, '_stock_status', 'instock');
update_post_meta($variation_id, '_height', '');
update_post_meta($variation_id, '_manage_stock', 'yes');
update_post_meta($variation_id, '_width', '');
update_post_meta($variation_id, '_sale_price_dates_from', '');
update_post_meta($variation_id, '_backorders', 'no');
update_post_meta($variation_id, '_stock_status', 'instock');
update_post_meta($variation_id, '_manage_stock', 'yes');
update_post_meta($variation_id, '_height', '');
update_post_meta($variation_id, '_width', '');
update_post_meta($variation_id, '_length', '');
update_post_meta($variation_id, '_weight', '');
update_post_meta($variation_id, '_downloadable', 'no');
update_post_meta($variation_id, '_virtual', 'no');
update_post_meta($variation_id, '_thumbnail_id', '0');
update_post_meta($variation_id, '_sku', '');
update_post_meta($variation_id, '_sale_price', $price_b);
update_post_meta($variation_id, '_price', $price_b);
update_post_meta($product_parent->id, '_min_variation_price', $price_b);
update_post_meta($product_parent->id, '_max_variation_price', $price_b);
update_post_meta($product_parent->id, '_min_price_variation_id', $variation_id);
update_post_meta($product_parent->id, '_max_price_variation_id', $variation_id);
update_post_meta($product_parent->id, '_min_variation_regular_price', $price_a);
update_post_meta($product_parent->id, '_max_variation_regular_price', $price_a);
update_post_meta($product_parent->id, '_min_regular_price_variation_id', $variation_id);
update_post_meta($product_parent->id, '_max_regular_price_variation_id', $variation_id);
update_post_meta($product_parent->id, '_min_variation_sale_price', $price_b);
update_post_meta($product_parent->id, '_max_variation_sale_price', $price_b);
update_post_meta($product_parent->id, '_min_sale_price_variation_id', $variation_id);
update_post_meta($product_parent->id, '_max_sale_price_variation_id', $variation_id);
update_post_meta($product_parent->id, '_price', $price_b);
update_post_meta($variation_id, '_stock', $stock);
update_post_meta($product_parent->id, 'post_status', 'publish');
¿Es necesario crear una imagen o simplemente imprimir posible opciones? – Peon
Ahora hay una API REST V2, recomendaría usar eso para crear productos. La API puede iniciar sesión como un usuario diferente y satisface todas las sutilezas que pueden pasarse por alto al construir un producto manualmente. También implica juntar una única estructura de datos grande y enviarla para que se configure en una sola acción. – Jason