You want to create Woocommerce products, and to do a good job you don't need only the post object and the thumbnail: you need taxonomies and custom fields, too: I think your product will have - at least - a price, isn't it?
Once you want to bulk create products I suppose that having same taxonomies and same custom fields for all of them is good for you (anyway manually changes are always possible).
I will suggest an approach, and give related code, but note is all not tested.
First of all, manually create one product: it will be your reference product.
Set everything for it: prices, variation, taxonomies... everything. Set also the thumbnail, off course.
Now just take note of the post ID of this product.
Second step is create a function that loops through your attachments and creates posts, assigns thumbnails, custom fields and taxonomies.
ADVICE: please note that woocommerce use some custom tables to save some informations, my method ignore this tables, so you will not bulk assign to posts information/settings about downloadable products. If you want create downloadable products from images you have to modify function before use it. Moreover some WooCommerce plugin use custom tables, too: be aware of this before running the function.
function wpa_convert_images_to_products($ref_id = 0, $skip_images = array() ) {
// following line ensure that present function is runned once
if ( get_transient('convert_images_to_products_done') ) return;
//try to remove php limits in execution time an memory
@set_time_limit (0);
@ini_set('memory_limit', -1);
if ( ! post_type_exists( 'product' ) || ! $ref_id )
wp_die('Reference post id is not valid or product post type is not registered.');
$reference = get_post($ref_id);
if ( ! $reference )
wp_die('Given reference post id is not valid.');
$ref_thumb = get_post_thumbnail_id( $ref_id );
if ( ! is_int($ref_thumb) ) $ref_thumb = null;
// get reference attributes
$product_vars = get_object_vars($reference);
unset($product_vars['ID']);
unset($product_vars['post_date']);
unset($product_vars['post_date_gmt']);
unset($product_vars['post_modified']);
unset($product_vars['post_modified_gmt']);
unset($product_vars['comment_count']);
// get reference custom fields
$ref_fields = get_post_custom( $ref_id );
// get reference taxonomies
$all_tax = get_object_taxonomies('product');
if ( ! empty($all_tax) ) {
$ref_tax = wp_get_object_terms( $ref_id, $all_tax, array('fields' => 'all') );
}
// skip reference thumbnail and images passed as second param in function
$skip_images = array_merge( (array)$skip_images, array($ref_thumb) );
$args = array(
'post__not_in' => $skip_images,
'post_type' => 'attachment',
'post_mime_type' => 'image',
'posts_per_page' => '-1',
'post_status' => 'inherit',
);
$images = new WP_Query( $args );
$errors = array();
// start loop through images
if ( $images->have_posts() ) :
// after that any other function call will fail
set_transient( 'convert_images_to_products_done', 1);
global $wpdb;
while( $images->have_posts() ) :
$images->the_post();
global $post;
$image = $post->ID;
$excerpt = get_the_excerpt();
if ( empty($excerpt) ) $excerpt = get_the_title();
$product_vars['post_title'] = get_the_title();
$product_vars['post_excerpt'] = $excerpt;
$product = wp_insert_post( $product_vars );
if ( intval($product) ) {
// insert custom fields
if ( ! empty($ref_fields) ) {
$meta_insert_query = "INSERT INTO $wpdb->postmeta ';
$meta_insert_query .= '(meta_key, meta_value) VALUES ";
$values = '';
foreach ( $ref_fields as $key => $array ) {
if ( $key != '_thumbnail_id' ) {
foreach ( $array as $value ) {
if ( $values != '' ) $values .= ', ';
$values .= $wpdb->prepare( '(%s, %s)', $key, maybe_serialize($value) );
}
}
}
if ( $values != '' ) {
$meta_insert_query .= $values;
if ( ! $wpdb->query( $meta_insert_query ) ) {
$error = 'Fail on inserting meta query for product ';
$error .= $product . '. Query: ' . $meta_insert_query;
$errors[] = $error;
}
}
}
// insert taxonomies
if ( ! empty($ref_tax) && ! is_wp_error( $ref_tax ) ) {
$taxonomies = array();
foreach ( $ref_tax as $term ) {
if ( ! isset($taxonomies[$term->taxonomy]) )
$taxonomies[$term->taxonomy] = array();
$taxonomies[$term->taxonomy][] = $term->slug;
}
foreach ( $taxonomies as $tax => $terms ) {
$set_tax = wp_set_post_terms( $product, $terms, $tax, false );
if ( ! is_array($set_tax) ) {
$error = 'Fail on insert terms of taxonomy ';
$error .= $tax . ' for product' . $product;
if ( is_string( $set_tax ) )
$error .= ' First offending term ' . $set_tax;
if ( is_wp_error($set_tax) )
$error .= ' Error: ' . $set_tax->get_error_message();
$errors[] = $error;
}
}
}
if ( ! set_post_thumbnail( $product, $image ) ) {
$error = 'Set thumbnail failed for product ';
$error .= $product . ' image ' . $image;
$errors[] = $error;
}
} else {
$errors[] = 'Insert post failed for image with id ' . $image;
}
endwhile;
else :
wp_die('You have no media.');
endif;
wp_reset_postdata();
if ( ! empty($errors) )
wp_die('<p>' . implode('</p><p>', $errors) . '</p>');
}
...Yes, it's a monster function...
Third and last step is the easiest one, you have to call the function just created and pass the reference product id as param. There are many way to do this task, here just one:
function launch_convert_images_to_products() {
//
// REPLACE THE 0 IN FUNCTION PARAM BELOW WITH YOUR REFERENCE PRODUCT ID
//
wpa_convert_images_to_products( 0 );
//
//
}
add_action('admin_init', 'launch_convert_images_to_products', 30);
Note that normally the function will create products for all your images. I added a second param to wpa_convert_images_to_products
function that can be an array of images id you want to skip.
Another way to create products from images selectively is create a custom taxonomy for attachments (info here) and then use appropriate tax_query
in the images WP_Query to select which images to convert.
Hope it helps.