In WooCommerce I am using Category and Taxonomy Image plugin that allow me to add the images to product attribute terms.
Now I am trying to display for a specific product attribute, the related term images for each product on shop page.
The author of Category and Taxonomy Image plugin metion to use the following code to display a term image:
if (function_exists('get_wp_term_image'))
{
$meta_image = get_wp_term_image($term_id);
//It will give category/term image url
}
echo $meta_image; // category/term image url
I am using the code below to display "color" product attribute term names on the shop page:
add_action('woocommerce_after_shop_loop_item','add_attribute');
function add_attribute() {
global $product;
$spec_val = $product->get_attribute('spec');
if(!empty($spec_val)) {
echo'<span class="view_attr"> SPECIFICATION: ' . $spec_val . '</span>';
}
}
How To display the term images?
Maybe this is the solution:
add_action('woocommerce_after_shop_loop_item','woo_new_product_tab_content');
function woo_new_product_tab_content() {
global $product;
$ingredients = $product->get_attributes( 'color' );
foreach( $ingredients as $attr_name => $attr ){
foreach( $attr->get_terms() as $term ){
if ( wc_attribute_label( $attr_name ) == "Color" ) {
echo $term->name ;
$meta_image = get_wp_term_image($term->term_id);
echo '<img src="'.$meta_image.'"/>';
}
else echo '';
}
}
}