Get Woocommerce Category Thumbnails
Asked Answered
Z

6

17

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?

Zsolway answered 28/11, 2012 at 13:25 Comment(3)
Can you please upload and update the working answerMytilene
In what way? The answer works, doesn't need any other code...Zsolway
Eh Jewel answered correctly and should be accepted, currently accepted answer by topherg is deprecatedCucumber
Z
32

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 );
Zsolway answered 28/11, 2012 at 16:1 Comment(3)
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.?Errol
Nevermind, got it. $image = wp_get_attachment_image_src( $thumbnail_id, $size ). Then $image[0]Errol
get_woocommerce_term_meta is depricated now (since wc 3.6.0). Use get_term_meta instead.Inefficacy
A
11

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.
Ardenardency answered 22/7, 2018 at 13:31 Comment(0)
S
11
<?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

Shopkeeper answered 12/5, 2019 at 13:17 Comment(1)
yes, just beware thumbnail_bid should be thumbnail_id ;)Inspissate
R
3

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..

Retention answered 7/3, 2013 at 19:58 Comment(0)
T
0

try this one

<?php 
    get_term_meta($term->term_id,'thumbnail_id',true);
?>
Tsimshian answered 1/9, 2022 at 7:4 Comment(2)
<?php get_term_meta($term->term_id,'thumbnail_id',true);?>Tsimshian
Remember that Stack Overflow isn't just intended to solve the immediate problem, but also to help future readers find solutions to similar problems, which requires understanding the underlying code. This is especially important for members of our community who are beginners, and not familiar with the syntax. Given that, can you edit your answer to include an explanation of what you're doing and why you believe it is the best approach?Stickseed
D
0

Try this code to solve the issue 'get_woocommerce_term_meta is deprecated since version 3.6!'

  • Install plugin Snippets
  • add this code as a php code
  • save
  • active

then check the product category page to solve the issue.

add_action( 'woocommerce_archive_description', 
function() {
if( ! is_product_category() ) { return; }
global $wp_query;
$cat = $wp_query->get_queried_object();
$thumbnail_id = get_woocommerce_term_meta( $cat- 
>term_id, 'thumbnail_id', true );
$image = wp_get_attachment_url( $thumbnail_id );
if( $image ) {
    echo sprintf( '<div style="background-image: 
url( \'%s\' );" /></div>', $image );
}
} );
Durwyn answered 21/12, 2023 at 8:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.