1

Using the Advanced Custom Fields plugin, I am trying to render an image on a single product page in WooCommerce.

I found this snippet of code from someone who is doing the same thing, but is rendering a text field instead. How would I change this to render an image?

add_action( 'woocommerce_single_product_summary', 'woocommerce_template_top_category_desc', 1 );
    function woocommerce_template_top_category_desc (){
    $terms = get_the_terms( $post->ID, 'wc-attibute-class' );
    if ( !empty($terms)) {
        $term = array_pop($terms);
                $text= get_field('txt-field', $term);
                if (!empty($text)) {
                echo $text;
                }
}
}

So far I have this, but it's not working. The add action is correct because I was using it for something else, but starting at $terms is where I get lost and is obviously not right.

add_action( 'woocommerce_product_thumbnails' , 'add_below_featured_image', 9 ); 
    function add_below_featured_image() {
    $terms = get_the_terms( $post->ID, '496' );
    if ( !empty($terms)) {
        $term = array_pop($terms);
                $image = get_field('banner_feedback', $term);
                if (!empty($image)) {
                echo $image;
                }
}
}
edit7279
  • 175
  • 5
  • 15

2 Answers2

2

Figured it out.

add_action( 'woocommerce_product_thumbnails' , 'add_below_featured_image', 9 ); 
function add_below_featured_image() {
    if( is_single( pageidhere ) ) {
    echo '<br><img src="urlhere">';
}
}
edit7279
  • 175
  • 5
  • 15
0

Steps to add custom gallery below featured image in woocommerce single product page(you can modify suitably to add just a single image):

  1. Create an ACF for Image Gallery.
  2. Copy the template for product-image.php from /plugins/woocommerce/templates/single-product to themes/your-child-theme/woocommerce/single-product/
  3. Add the following code to product-image.php just before the closing div of "images"
  4. Add the images you want in the gallery to your single product admin page in the ACF custom field.

`

<?php $images = get_field('product_image_gallery'); ?>
<div class="mycustom_image_gallery">
<?php if($images): ?>
    <div class="popup-gallery">
            <?php foreach( $images as $image ): ?>
                <a href="<?php echo $image['url']; ?>"
                   class="lightbox-link"
                   title="<?php echo $image['caption']; ?>"
                   data-description="<?php echo $image['description']; ?>">
                    <div class="image-wrap">
                        <img src="<?php echo $image['sizes']['thumbnail']; ?>">
                    </div>
                </a>
            <?php endforeach; ?>
    </div>

<?php endif; ?>
</div>

`

Rahul Madhavan
  • 304
  • 3
  • 10