I want to add some extra images to the product gallery. Based on a meta field from a different post. The images should only add to the gallery in the frontend. Not to the image gallery of the product itself.
EDIT: @mujuonly was right. I could use woocommerce_product_thumbnails
and so I tried the following code and everything works fine:
<?php add_action( 'woocommerce_product_thumbnails', 'custom_product_add_thumbnails', 100, 0 ); function custom_product_add_thumbnails(){ $extra_gallery = get_field( 'some_field' ); if( $extra_gallery ): ?> <?php foreach ( $extra_gallery as $image_id ): ?> <div class="woocommerce-product-gallery__image"> <?php echo wp_get_attachment_image( $image_id, 'woocommerce_single', false, array( "class" => "" ) ); ?> </div> <?php endforeach; ?> <?php endif; endif; }
With 100
in the action the images are shown at the end of the gallery:
add_action( 'woocommerce_product_thumbnails', 'custom_product_add_thumbnails', 100, 0 );
Advertisement
Answer
add_action( 'woocommerce_product_thumbnails', 'custom_product_add_thumbnails', 100, 0 ); function custom_product_add_thumbnails(){ // Echo images here }