'Add multiple images to WooCommerce product

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));


Solution 1:[1]

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.

Solution 2:[2]

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);
    }

Solution 3:[3]

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 );
}

Solution 4:[4]

This code will upload multiple images in product 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);
          }

Solution 5:[5]

This code will update the product gallery when a new image is loaded to the product. I', using it in a React app that uses wp as back-end, the new image is loaded trough the app and then the hook "add action" triggers the code that includes the image into the product gallery.

I use this code in the functions.php file:

<?php
  add_action( 'add_attachment', 'auto_on_upload' ); 

  function auto_on_upload( $attachment_id ) { 
    $parent = get_post_ancestors( $attachment_id );
    $images = get_attached_media('', $parent[0]);
    foreach( $images as $key => $image) {
      $images_array[] = $key; 
    };
    $images_array[] = $attachment_id;
    update_post_meta($parent[0], '_product_image_gallery',  implode(',', $images_array));
} 
?>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Frits
Solution 2 Yurii Vlasenko
Solution 3 EssGee
Solution 4 Emi OB
Solution 5