I need get woo product gallery second img

i need get woo product gallery second img, how to use dynamic data to set it

Hi there,

You can do that using a custom function.

I create a small tutorial for you :

1- Add the following code in the functions.php file from your Bricks Child Theme:

// GET WOOCOMMERCE PRODUCT GALLERY SECOND IMAGE
function woo_product_second_image() {
    global $product;
    $attachment_ids = $product->get_gallery_attachment_ids();
    
    // Check if there are at least two images in the gallery
    if (isset($attachment_ids[1])) {
        return $attachment_ids[1]; // Return the second image ID
    }
    
    return null; // Return null if there is no second image
}

If you want to have the whole gallery instead of the second image only, you can simply use this code:

// GET WOOCOMMERCE PRODUCT GALLERY
function woo_product_images() {
 global $product; 
 $attachment_ids = $product->get_gallery_attachment_ids(); 
 return $attachment_ids;
}

2- Add the following code in the functions.php file from your Bricks Child Theme:
Indeed, and since Bricks 1.9.7, functions must pass the check through the bricks/code/echo_function_names filter.

// ALLOW CUSTOM FUNCTIONS IN BRICKS
add_filter( 'bricks/code/echo_function_names', function() {
  return [
    'woo_product_second_image',
	'woo_product_images',
  ];
} );

3- Use the code wherever you want. For example in the Image Gallery or Image element:

image

Have a great day,
Thomas

2 Likes

thanks for that :kissing_heart:

work well

1 Like

You are hero!
I think most people want the first image of the “product gallery”, just change “$attachment_ids[1]” to “$attachment_ids[0]”

Good man! Thank you very much, works like a charm.