1
votes

How can I assign multiple images to a product in WooCommerce?

I've tried:

update_post_meta( $post_id, '_product_image_gallery', $image_id);

but it assigns only one image. When I use an array, it does not work:

update_post_meta( $post_id, '_product_image_gallery', array($image_id,$image_id2));
4

4 Answers

5
votes

If you have multiple images that need to be assigned to a product, you will need to assign one image as the featured image/thumbnail, and then assign the rest of the images as the product gallery thumbnails.

Below is a quick function that can achieve this for you:

function so_upload_all_images_to_product($product_id, $image_id_array) {
    //take the first image in the array and set that as the featured image
    set_post_thumbnail($product_id, $image_id_array[0]);

    //if there is more than 1 image - add the rest to product gallery
    if(sizeof($image_id_array) > 1) { 
        array_shift($image_id_array); //removes first item of the array (because it's been set as the featured image already)
        update_post_meta($product_id, '_product_image_gallery', implode(',',$image_id_array)); //set the images id's left over after the array shift as the gallery images
    }
}

Assuming you have an array of image id's, and the id of the product that you want to attach the images to, you can call the function above like this:

$images = array(542, 547, 600, 605); //array of image id's
so_upload_all_images_to_product($product_id, $images);

If you are working with a massive array of product images, or you are very serious about micro-optimisation, you can use a combination of array_reverse and array_pop instead of array_shift.

1
votes

Try like this:

update_post_meta( $post_id, '_product_image_gallery', $image_id.",". $image_id2);

Example to complete:

$images_ids = array();
$images_meta = "";
    foreach ($images_ids as $im_id) {
      if (is_null(get_post_meta($post_id,"_product_image_gallery")))
        add_post_meta($post_id,"_product_image_gallery",$im_id);
      else {
        $images_meta = get_post_meta($post_id,"_product_image_gallery",true);             
       update_post_meta($post_id,"_product_image_gallery",$images_meta.",".$im_id);
    }
0
votes

Here is a little wrapper for this job. It accepts only one attachment and adds it either as the base image or adds to the gallery if the product has a base image already.

/**
 * @param $product WC_Product|WP_Post
 * @param $attachmentId int
 */
function setOrAddImageToProduct($product, $attachmentId)
{
    if(!has_post_thumbnail($product->ID)) {
        set_post_thumbnail( $product, $attachmentId );
    } else {
        $gallery = get_post_meta($product->ID, '_product_image_gallery');
        if(!empty($gallery)) {
            $galleryItems = explode(",", $gallery);
            $galleryItems[] = $attachmentId;
        } else {
            $galleryItems = [$attachmentId];
        }
        update_post_meta($product->ID, '_product_image_gallery', join(',', $galleryItems));
    }

    //Adds connection to the product for the media view
    $attachment = get_post( $attachmentId );
    $attachment->post_parent = $product->ID;
    wp_update_post( $attachment );
}
0
votes

This code will upload multiple images in produt gallery

foreach ($files['name'] as $key => $value) { if ($files['name'][$key]) {

                  $file = array( 
                      'name' => $files['name'][$key],
                      'type' => $files['type'][$key], 
                      'tmp_name' => $files['tmp_name'][$key], 
                      'error' => $files['error'][$key],
                      'size' => $files['size'][$key]
                  ); 
                  $_FILES = array ("my_file_upload" => $file);
                  $newupload = my_handle_attachment( "my_file_upload", $post_id);
              } 
          $attach_newupload_ids[]=$newupload;
      }
      $attach_ids=implode(',',$attach_newupload_ids);
      if($attach_ids){
        update_post_meta( $post_id, '_product_image_gallery', $attach_ids);
      }