I am using the plugin WooCommerce Variation Swatches and Photos which lets me add a thumbnail to my product's attributes.
I need to list all the attributes on a template and I would like to also get and show the thumbnail.
$terms = get_terms( array(
'taxonomy' => 'pa_texture',
'hide_empty' => false,
) );
foreach ( $terms as $term ) {
print_r($term);
}
The thumbnail feature is not default in WooCommerce so when I print_r $term there is no thumbnail URL:
WP_Term Object
(
[term_id] => 54
[name] => Guilin
[slug] => guilin
[term_group] => 0
[term_taxonomy_id] => 54
[taxonomy] => pa_texture
[description] => Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla imperdiet facilisis convallis.
[parent] => 0
[count] => 2
[filter] => raw
[meta_value] => 0
)
How can I get the attribute's thumbnail image?
Found the solution thanks to @Und3rTow's input.
The correct parameter in get_woocommerce_term_meta is pa_texture_swatches_id_photo
.
Here is the final code:
$thumbnail_id = get_woocommerce_term_meta( $term->term_id, 'pa_texture_swatches_id_photo', true );
$textureImg = wp_get_attachment_image_src( $thumbnail_id ); ?>
<img src="<?php echo $textureImg[0]; ?>">