Company logo
  • Jobs
  • Bootcamp
  • About Us
  • For professionals
    • Home
    • Jobs
    • Courses
    • Questions
    • Teachers
    • Bootcamp
  • For business
    • Home
    • Our process
    • Plans
    • Assessments
    • Payroll
    • Blog
    • Calculator

0

58
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.

9 months 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);
            }
9 months ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post job Plans Our process Sales
Legal
Terms and conditions Privacy policy
© 2023 PeakU Inc. All Rights Reserved.