Come ottenere le immagini della galleria dai prodotti in WooCommerce?

20 ago 2017, 09:54:26
Visualizzazioni: 47.2K
Voti: 10

Sto cercando un codice che possa aiutarmi a ottenere l'elenco delle immagini della galleria presenti nel prodotto in WooCommerce.

Ho bisogno di queste per poterle utilizzare in un design personalizzato della pagina singola del prodotto WooCommerce.

0
Tutte le risposte alla domanda 3
3
22
<?php
$product_id = '14';
$product = new WC_product($product_id);
$attachment_ids = $product->get_gallery_image_ids();

foreach( $attachment_ids as $attachment_id ) 
    {
      // Mostra l'URL dell'immagine
      echo $Original_image_url = wp_get_attachment_url( $attachment_id );

      // Mostra l'immagine invece dell'URL
      echo wp_get_attachment_image($attachment_id, 'full');

    }?>
1 dic 2019 22:19:31
Commenti

Dovresti cercare di spiegare il tuo codice, sarebbe utile.

RiddleMeThis RiddleMeThis
1 dic 2019 23:57:42

OK certo, posso farlo.

Md.Mehedi hasan Md.Mehedi hasan
2 dic 2019 00:06:51

Questa è la risposta più semplice e migliore.

RiotAct RiotAct
11 dic 2019 02:48:39
0

Il metodo get_gallery_image_ids() sul prodotto restituirà un array di ID immagine.

global $product;
$gallery_images = $product->get_gallery_image_ids();

Poi puoi usare funzioni come wp_get_attachment_image() per ogni ID per ottenere HTML/URL ecc.

20 ago 2017 11:35:48
0

Questa è la funzione completa se vuoi ottenere tutte le immagini dalla galleria, inclusa l'immagine principale:

    public function getAllProducts( $request ){
    $products = new WP_Query([
        'post_type' => 'post'//,
            //'show_product_on_only_premium' => 'yes',
        ]);

    $args = array(
        'status' => 'publish',
    );
    $products = wc_get_products( $args );
    $tempArr = [];
    foreach($products as $product){
        $product_obj = json_decode($product-> __toString());
        $product_obj->img_src = wp_get_attachment_image_src($product_obj->image_id)[0];
        $images_ids = $product-> get_gallery_image_ids();

        $images_arr = [];
        for($i = 0, $j = count($images_ids); $i < $j;$i++ ){
            $image_query = wp_get_attachment_image_src($images_ids[$i]);
            $img = new StdClass;
            $img->src = $image_query[0];
            array_push($images_arr, $img);
        }
        $product_obj->gallery = $images_arr;
        array_push($tempArr, $product_obj);
    }
    return $tempArr;
} 

E se vuoi le varianti: da: https://gist.github.com/Niloys7/17b88d36c1c38844a6cf2127c15dee63

<?php
global $product;
$attachment_ids = $product->get_gallery_attachment_ids();

foreach( $attachment_ids as $attachment_id ) 
{
  //Ottieni URL delle immagini della galleria - dimensioni standard di WordPress
  echo $Original_image_url = wp_get_attachment_url( $attachment_id );
  echo $full_url = wp_get_attachment_image_src( $attachment_id, 'full' )[0];
  echo $medium_url = wp_get_attachment_image_src( $attachment_id, 'medium' )[0];
  echo $thumbnail_url = wp_get_attachment_image_src( $attachment_id, 'thumbnail' )[0];

  //Ottieni URL delle immagini della galleria - dimensioni specifiche di WooCommerce
  echo $shop_thumbnail_image_url = wp_get_attachment_image_src( $attachment_id, 'shop_thumbnail' )[0];
  echo $shop_catalog_image_url = wp_get_attachment_image_src( $attachment_id, 'shop_catalog' )[0];
  echo $shop_single_image_url = wp_get_attachment_image_src( $attachment_id, 'shop_single' )[0];

  //Mostra l'immagine invece dell'URL
  echo wp_get_attachment_image($attachment_id, 'full');
  echo wp_get_attachment_image($attachment_id, 'medium');
  echo wp_get_attachment_image($attachment_id, 'thumbnail');
  echo wp_get_attachment_image($attachment_id, 'shop_thumbnail');
  echo wp_get_attachment_image($attachment_id, 'shop_catalog');
  echo wp_get_attachment_image($attachment_id, 'shop_single');
}
?>
14 ago 2018 02:33:32