WooCommerce: ottenere le immagini associate ai prodotti

WooCommerce: ottenere le immagini associate ai prodotti

In WooCommerce possiamo ottenere tutte le immagini associate ad un singolo prodotto.

La soluzione è la seguente:


function my_get_wc_images( $product_id ) {

  $product = new WC_product( $product_id );
  $attachments = $product->get_gallery_attachment_ids();
  $urls = array();
  $feat = wp_get_attachment_image_src( get_post_thumbnail_id( $product_id ), 'medium' );
  $feat2 = wp_get_attachment_image_src( get_post_thumbnail_id( $product_id ), 'thumbnail' );
  $urls[] = array(
    'full' => $feat[0],
    'thumb' => $feat2[0]
  );
  foreach( $attachments as $id ) {
    $img = wp_get_attachment_image_src( $id, 'medium' );
    $thumb = wp_get_attachment_image_src( $id, 'thumbnail' );
    $urls[] = array(
       'full' => $img[0],
       'thumb' => $thumb[0]
    );
  }

	return $urls;
}

Torna su