WooCommerce: ottenere tutte le immagini collegate ad un prodotto

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

WordPress di Gabriele Romanato

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

La soluzione รจ la seguente:


<?php
function my_get_wc_images( $productId ) {

	$product = new WC_product( $productId );
  $attachments = $product->get_gallery_attachment_ids();
  $urls = array();
  $feat = wp_get_attachment_image_src( get_post_thumbnail_id( $productId ), 'medium' );
  $feat2 = wp_get_attachment_image_src( get_post_thumbnail_id( $productId ), '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;
}

Commenti