Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

103
Vistas
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.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

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);
            }
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda