Problem with inserting multiple images in gallery of each WooCommerce product programmatically

I am creating a script to programmatically add multiple images to the gallery of each WooCommerce product.

The script works but only takes the first image, I'm having a hard time figuring out how to save each array containing gallery images then move on to the next one.

Obviously I tried several times with the for loop but I always have errors.

Here's the var dump of my code (Without Wordpress functions):

 array(2) {
  [0]=
  string(47) e8d753_4d28f4185e754761aaedee75b710f4b7~mv2.jpg
  [1]=
  string(47) e8d753_2b34ab0d9ae84c84a50ea7a71e3b17b2~mv2.jpg
}
array(1) {
  [0]=
  string(0) 
}
array(1) {
  [0]=
  string(0) 
}
array(1) {
  [0]=
  string(0) 
}
array(2) {
  [0]=
  string(47) e8d753_c28f96d0262b4d8cacffeb032d08a66e~mv2.jpg
  [1]=
  string(47) e8d753_12e415672b404c6db915a92e5870a5c0~mv2.jpg
}

And here's the code:

?php 

   function generate_Featured_Image( $image_url, $post_id  ){
            $upload_dir = wp_upload_dir();
            $image_data = file_get_contents($image_url);
            $filename = basename($image_url);
            if(wp_mkdir_p($upload_dir['path']))
              $file = $upload_dir['path'] . '/' . $filename;
            else
              $file = $upload_dir['basedir'] . '/' . $filename;
            file_put_contents($file, $image_data);
        
            $wp_filetype = wp_check_filetype($filename, null );
            $attachment = array(
                'post_mime_type' = $wp_filetype['type'],
                'post_title' = sanitize_file_name($filename),
                'post_content' = '',
                'post_status' = 'inherit'
            );
            // Image gallery
            $attach_id = wp_insert_attachment( $attachment, $file, $post_id );
            require_once(ABSPATH . 'wp-admin/includes/image.php');
            $attach_data = wp_generate_attachment_metadata($attach_id, $file); //asign the meta
            wp_update_attachment_metadata($attach_id, $attach_data); //update the post
            update_post_meta($post_id, '_product_image_gallery', array_push($attach_id));
        }

        
        foreach ($items as $it) {
            

            $title = array_key_exists(2, $it) ? $it[2] : null;
            $description = array_key_exists(3, $it) ? $it[3] : null;
            // Multiple images in each key
            $img = array_key_exists(4, $it) ? $it[4] : null;
            $secimg = explode(';', $img);
            $cat = array_key_exists(5, $it) ? $it[5] : null;
            $price = array_key_exists(8, $it) ? $it[8] : null;
            $stock = array_key_exists(6, $it) ? $it[6] : null;
            
            
            $postarr = array(
                'post_title' = wp_strip_all_tags($title),
                'post_name' = $title,
                'post_content' = $description,
                'post_type' = 'product',
                'post_status' = 'publish',
            );
            $insert_id = wp_insert_post($postarr, true);

            $terimg = array_reverse($secimg);
            foreach ($terimg as $ru) {
                $sol = 'https://static.wixstatic.com/media/';
                $sol .= $ru;
                generate_Featured_Image($sol, $insert_id);
            }


            // End script 1
                        
            wp_set_object_terms($insert_id, $cat, 'product_cat');

            //set product type
            wp_set_object_terms($insert_id, 'simple', 'product_type');

            update_post_meta( $insert_id, '_price', $price );
            update_post_meta($insert_id, '_sku', $stock);
            update_post_meta($insert_id, 'total_sales', '0');
            
        }

Thank you for your help !

Topic array uploads php attachments plugin-development Wordpress

Category Web

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.