Get Woocommerce Category Thumbnails

45,224

Solution 1

Sorted it, here's the code I used:

$thumbnail_id = get_woocommerce_term_meta( $cat->term_id, 'thumbnail_id', true );
$image = wp_get_attachment_url( $thumbnail_id );

Solution 2

If the get_woocommerce_term_meta() function does not work for you then you can try the get_term_meta()function instead.

You can get the WooCommerce product category thumbnail with the following code-

<?php
$thumbnail_id = get_term_meta( $cat->term_id, 'thumbnail_id', true );
$image_url = wp_get_attachment_url( $thumbnail_id ); // This variable is returing the product category thumbnail image URL.

Solution 3

<?php
$thumbnail_id = get_term_meta( $cat->term_id, 'thumbnail_id', true );
$image_url    = wp_get_attachment_url( $thumbnail_id ); // This variable is returing the product category thumbnail image URL.

Notice: get_woocommerce_term_meta is deprecated

Solution 4

Had a similar setup but when I used what you did I didnt actually get the thumbnail file I got the full image file so instead I used this: wp_get_attachment_thumb_url so that my output url would be "../my-images"/image-150x150.jpg" and actually got it to pull the thumbnail image, just incase anyone runs into a similar situation..

Share:
45,224
topherg
Author by

topherg

Updated on May 25, 2021

Comments

  • topherg
    topherg almost 3 years

    I have a custom template for a woocommerce category page to only display the categories. I have got the system to get a list of the child categories (by using get_term_children($id, 'product_cat') and get_term_by(...)), but it only returns objects containing all the required information, except the thumbnail data. Does anyone know how I can get the thumbnail for the term?

  • Joel Worsham
    Joel Worsham about 9 years
    But that's the full-size image. Does WooCommerce not provide all image sizes like a normal post attachment does? What if I want to get the actual thumbnail size, or large size, etc.?
  • Joel Worsham
    Joel Worsham about 9 years
    Nevermind, got it. $image = wp_get_attachment_image_src( $thumbnail_id, $size ). Then $image[0]
  • Nikolay Sergeevich
    Nikolay Sergeevich over 3 years
    get_woocommerce_term_meta is depricated now (since wc 3.6.0). Use get_term_meta instead.
  • Fanky
    Fanky over 3 years
    yes, just beware thumbnail_bid should be thumbnail_id ;)