How to add multiple images in _product_image_gallary using php

I need to copy images from a remote server and add multiple values to _product_gallary_image post meta in WordPress. For example it should be like that;

_product_image_gallary | 123,456,124,786,653

Everything is working fine except all image ids are not added only last image is added.

Read More

When I use add_post_meta in foreach loop its adding all images in seperate row like:

205 _product_image_gallary 234
205 _product_image_gallary 235
205 _product_image_gallary 236

This is my code:

foreach  ( $extra_images as $extra_image ) { 
    // Download file to temp location
    $tmp2  =  download_url( $extra_image )  ;
    // fix file name for query strings
    preg_match('/[^?]+.(jpg|JPG|jpe|JPE|jpeg|JPEG|gif|GIF|png|PNG)/', $extra_image, $matches)  ;
    $file_array2['name'] = basename( $matches[0] )  ;
    $file_array2['tmp_name']  =  $tmp2  ;
    $imgID = media_handle_sideload( $file_array2 , $post_id , 'desc' );
    $imagearray[]  =  $imgID ;
}
$imgids  =  implode(' , ', $imagearray ) ;
$result  =  update_post_meta( $post_id, '_product_image_gallery', $imgids ) ;

Related posts