Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

131
Views
Add/Set product Images and gallery images programmatically

I'm getting data from jSON file and trying to setup the images for a product, My goal is - need to develop a way to extract/download images from JSON responses and set images to related product //$product_data is getting the response from Json

$product_data = $_REQUEST['data'];
$product = new WC_Product_Simple();
$product->set_name( $product_data['Title'] ); // it's working
$product->set_status( 'publish' );
$product->save();

If it has multiple images that need to be assigned to a product, the first image need to assign as the featured image/thumbnail, and then assign the rest of the images as the product gallery thumbnails.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

$product_images = [];
            $count = 0;
            foreach($product_data as $product_key => $product_value):
                if (strpos($product_key, 'Image') !== false):

                    $product_img_upload = wc_rest_upload_image_from_url(esc_url_raw($product_value));
                    $product_img_id = wc_rest_set_uploaded_image_as_attachment($product_img_upload, $product_data);

                    // Add image ID to array.
                    $product_images[] = $product_img_id;

                    $count++;

                endif;
            endforeach;

            if (!empty( $product_images ) ) {
                $product->set_image_id( $product_images[0] );
                array_shift($product_images);
                $product->set_gallery_image_ids( $product_images);
            }
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!