Baptiste
Baptiste

Reputation: 1

Woocommerce additionnal variation images - How to display the second image

For the purpose of a product image flipper, I want to display the second image from the variation images for each product.

I'm using the WooCommerce Additional Variation Images plugin and I don't know how to get that image.

I tried this

$product->wp_get_attachment_image_src( $id )

but it returns the following : Call to undefined method WC_Product_Variation::wp_get_attachment_image_src() in...

Anyone who is familiar with this plugin could help me ? If you need more information let me know.

Thanks !

Upvotes: 0

Views: 1905

Answers (2)

Vpant
Vpant

Reputation: 361

You can get the additional variation images IDs and use these IDs to get the image src like this:

$attachment_ids = get_post_meta( key($values), '_wc_additional_variation_images', true );
$variation_images_src = array();

if ( $attachment_ids ) {
  foreach ( $attachment_ids as $attachment_id ) {
    $variation_images_src[] = wp_get_attachment_image_src($attachment_id)
  }
}

Upvotes: 1

Alex
Alex

Reputation: 2775

You can get all images for all product variations by this code:

$variations = $product->get_available_variations();
foreach ( $variations as $variation ) {
    echo $variation['image_src'];
}

Upvotes: 0

Related Questions