skip to Main Content

I’m struggling to populate ACF product_tag taxonomy images into product pages. Each product may be assigned to 1 or more product tag. I’d like to display those images and make the link to their archive pages.

ACF screenshot

Product Tag screenshot

I have this code but it is only showing the word "Tag". Can’t make it pull the images:

add_action('woocommerce_before_add_to_cart_form','finish_image',22);
function finish_image() {
$terms = get_the_terms( $post->ID , 'product_tag' );
if($terms) {
foreach( $terms as $term ) {
$image = get_field('tag-image');
echo 'Tag <img src="'.$image[0].'" alt="" />';
}
}
}

Anyone knows about this, please?

Regards!

2

Answers


  1. Chosen as BEST ANSWER

    For whoever needs it, here is the final code thanks to @DrashtiRajpura

    add_action('woocommerce_before_add_to_cart_form','finish_image');
    function finish_image() {
    global $product;
    $terms = get_the_terms( $product->get_id() , 'product_tag' );
    if($terms) {
    foreach( $terms as $term ) {
    $image = get_field('tag-image', $term);
    $url = get_term_link($term->term_id);
    echo '<a href="'.$url.'"><img src="'.$image.'" alt="" style="width: 100px; border: none; margin-right: 30px;"/></a>';
    }
    }
    }
    

  2. Add term id as second parameter and use global $product, then try if you get image url

        add_action('woocommerce_before_add_to_cart_form','finish_image');
    function finish_image() {
    global $product;
    $terms = get_the_terms( $product->get_id() , 'product_tag' );
    if($terms) {
    foreach( $terms as $term ) {
    $image = get_field('tag-image', $term);
    $url = get_term_link($term->term_id);
    echo '<a href="'.$url.'"><img src="'.$image.'" alt="" style="width: 100px; border: none; margin-right: 30px;"/></a>';
    }
    }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search