I am trying to create product variation in woocommerce and using following code for insert variation post type.
foreach ($book_tag as $bookTag) {
$post_name = 'ABE-'.$post_id.'book-type'.$bookTag;
$my_post = array(
'post_title' => 'BookType ' . $bookTag . ' 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 = wp_insert_post($my_post);
update_post_meta($attID, 'book_type', $bookTag);
update_post_meta($attID, '_price', $bookPrice);
update_post_meta($attID, '_regular_price', $bookPrice);
update_post_meta($attID, '_sale_price', $salePrice );
update_post_meta($attID, '_sku', $post_name);
update_post_meta($attID, '_virtual', 'no');
update_post_meta($attID, '_downloadable', 'yes');
update_post_meta($attID, '_manage_stock', 'no');
update_post_meta($attID, '_stock_status', 'instock');
}
This is working fine but it is not set auto book type, i have to select attribute from the drop down option but i want it should auto selected attribute according attribute values. Following screenshot which is showing to me:
but i want like following screenshot
So can you help me how do set attribute for variation products, my code is perfect or something do else.