skip to Main Content

There’s a number of posts on here attempting to solve this however, I haven’t been able to find a solution that works.

I’m attempting to get the URL of the thumbnail image, associated with the product attribute term of the product displayed on a single-product page, and then display the thumbnail below the product meta, with a URL to the term’s archive.

The hook that I’m using is as follows:

add_action('woocommerce_product_meta_end','product_brand_image');
   

Then I’m defining the function used in the hook:

function product_brand_image() {
} 

The attribute name is "Brand", and the slug is "brand".

I’ve managed to get as far as getting the term ID using the following:

function product_brand_image() {
    global $product;
    $attributes = $product->get_attributes();
    $attr_id = $attributes['pa_brand']['options'][0];
} 

I’ve prnt’d this and it returns the term ID, which I’ve confirmed to be correct.

I’m struggling to now use this to get the associated term thumbnail URL, and term archive slug to be able to insert the thumbnail and then link it to the archive.

Looking to solve this programmatically without using another plugin.

UPDATE:

Ok, so using @Barrie Dawson comment as a guide, I am now up to this point:

function product_brand_image() {
    global $product;
    $attributes = $product->get_attributes();
    $attr_id = $attributes['pa_brand']['options'][0];
    $termMeta = get_term_meta($attr_id);
    if (is_array($termMeta) && array_key_exists('product_search_image_id', $termMeta)) {
        $post_id = $termMeta['product_search_image_id'][0];
        $postData = get_post($post_id);
    }
} 

I have found that attribute term thumbnails are stored in table wp_posts. The ‘product_search_image_id’ field links to the ID field in wp_posts table. The URL of the thumbnail associated to the term is then listed under the ‘guid’ column in this same table. I need some assistance with the PHP to extract this.

2

Answers


  1. Chosen as BEST ANSWER

    And here is the final solution I arrived at:

    add_action('woocommerce_product_meta_end','product_brand_image');
    
    function product_brand_image() {
        global $product;
        $attributes = $product->get_attributes();
        $attr_id = $attributes['pa_brand']['options'][0];
        $term = get_term($attr_id);
        $termSlug = $term->slug;
        $termMeta = get_term_meta($attr_id);
        if (is_array($termMeta) && array_key_exists('product_search_image_id', $termMeta)) {
            $post_id = $termMeta['product_search_image_id'][0];
            $postData = get_post($post_id);
            $prod_image_url = $postData->guid;
                echo '<span class="product_brand_image"><a href="/brand/'.$termSlug.'"><img src="'.$prod_image_url.'" /></a></span>';
        }
    }
    

    This displays the thumbnail of the term of the product attribute below the product meta, and links it to the archive page for that term. In this instance, it links to a shop page (archive) with all products associated to a particular 'brand'. See screenshot below.

    Screenshot


  2. <?php
    
    function product_brand_image() {
        global $product;
        $attributes = $product->get_attributes();
        $attr_id = $attributes['pa_brand']['options'][0];
        $termMeta = get_term_meta($attr_id);
        if (is_array($termMeta) && array_key_exists('thumbnail_id', $termMeta)) {
            $termThumbnail = get_the_post_thumbnail_url($termMeta['thumbnail_id']);
        }
    } 
    

    you can also request available sizes see: https://developer.wordpress.org/reference/functions/get_the_post_thumbnail_url/

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search