ylliX - Online Advertising Network

Quick tip: How to create WooCommerce product variants programmatically?

I found few solutions, but obviously none of them were working, so had to find it out by myself. So where to start – lets suppose we have our $post_id somewhere (I’m pretty sure there are lot of tutorials how to get post from database), so in first step we need to modify some base product settings:

wp_set_object_terms($post_id, 'variable', 'product_type');
wp_set_object_terms($post_id, implode('|', $availableColors), 'color');

So what happens here? In first line we are changing our product type to variants (you can do it also in product edit page in woocommerce), next line adds all possible colors we need (this can be any attribute like size, model or everything you want), $availableColors is just plain php array. Next step is:

$product_attributes = array();
$product_attributes['color'] = array(
	'name' => 'Color',
	'value' => implode('|', $availableColors),
	'position' => 0,
	'is_visible' => 0,
	'is_variation' => 1,
	'is_taxonomy' => 0
);
update_post_meta($post_id, '_product_attributes', $product_attributes);

Code above adds our color attribute to base product also with all possible values. Which is important here – is_visible should be set to 0 and is_variation should be set to 1. Then:

foreach ($availableColors as $color) {
	$colorClean = preg_replace("/[^0-9a-zA-Z_-] +/", "", $color);
	$post_name = 'product-' . $post_id . '-color-' . $colorClean;
	$my_post = array(
		'post_title' => 'Color ' . $color . ' for #' . $post_id,
		'post_name' => $post_name,
		'post_status' => 'publish',
		'post_parent' => $post_id,
		'post_type' => 'product_variation',
		'guid' => home_url() . '/?product_variation=' . $post_name
	);
	$attID = $wpdb->get_var("SELECT count(post_title) FROM $wpdb->posts WHERE post_name like '$post_name'");
	if ($attID < 1) {
		$attID = wp_insert_post($my_post);
	}
	update_post_meta($attID, 'attribute_color', $color);
	update_post_meta($attID, '_price', 100);
	update_post_meta($attID, '_regular_price', 100);
	update_post_meta($attID, '_sku', $post_name);
	update_post_meta($attID, '_virtual', 'no');
	update_post_meta($attID, '_downloadable', 'no');
	update_post_meta($attID, '_manage_stock', 'no');
	update_post_meta($attID, '_stock_status', 'instock');
}

The last part is to insert or update your variation for each defined color. The variable $post_name can be anything you want, just should be same schema for all variants as we are using it to check if such variant already exists and should be only updated, or not exists and should be created. But you can use also SKU or GUID for this.

16 thoughts on “Quick tip: How to create WooCommerce product variants programmatically?

  1. Thanks for this tutorial. You saved me. There are many articles regarding this but none of them worked as yours. Thank you. Keep making woocommerce tutorials. We need this.

  2. All good, but variants are not properly added. I have 3 size attributes. only first one is added. Also not displaying properly in site.

      1. but then what is the need of coding that can be done by excel. I need help on this topic “I need to create attribute dynamically so that when someone add new product in woocommerce manually then automatically attributes and variation will create. Thanks please help me soon.

Leave a Reply to Zarar AnsariCancel reply