I was using the following code to set images for each attribute term and it worked just fine only if there was just one term assigned to each product:
function tutsplus_list_attributes( $product ) {
global $product;
$style = array_shift( wc_get_product_terms( $product->id, 'pa_style', array( 'fields' => 'names' ) ) );
$new = array_shift( wc_get_product_terms( $product->id, 'pa_other', array( 'fields' => 'names' ) ) );
echo '<div class="att">';
if ($style == 'Red Wine') {
echo '<img src="' . get_template_directory_uri() .'/img/red-wine-icon.png" alt="Red Wine" width="35" height="35"> Red';
}
if ($style == 'White Wine') {
echo '<img src="' . get_template_directory_uri() .'/img/white-wine-icon.png" alt="White Wine" width="35" height="35"> White';
}
if ($style == 'Rosé Wine') {
echo '<img src="' . get_template_directory_uri() .'/img/rose-wine-icon.png" alt="Rosé Wine" width="35" height="35"> Rosé';
}
if ($style == 'Sweet') {
echo '<img src="' . get_template_directory_uri() .'/img/sweet-wine-icon.png" alt="Sweet Wine" width="35" height="35"> Sweet';
}
if ($new == 'New Wines') {
echo '<img src="' . get_template_directory_uri() .'/img/new-wine-icon.png" alt="New Wine" width="35" height="35">';
}
echo '</div>';
}
add_action( 'woocommerce_single_product_summary', 'tutsplus_list_attributes', 0 );
But obviously, some products will require more than one term i.e. New, White Wine, Sweet.
I've tried adding a foreach loop to to make it work with multiple attribute terms but now it doesn't show anything:
function tutsplus_list_attributes( $product ) {
global $product;
$style = array_shift( wc_get_product_terms( $product->id, 'pa_style', array( 'fields' => 'names' ) ) );
$new = array_shift( wc_get_product_terms( $product->id, 'pa_other', array( 'fields' => 'names' ) ) );
echo '<div class="att">';
foreach ( $style as $s) {
if ($style == 'Red Wine') {
echo '<img src="' . get_template_directory_uri() .'/img/red-wine-icon.png" alt="Red Wine" width="35" height="35"> Red';
}
elseif ($style == 'White Wine') {
echo '<img src="' . get_template_directory_uri() .'/img/white-wine-icon.png" alt="White Wine" width="35" height="35"> White';
}
elseif ($style == 'Rosé Wine') {
echo '<img src="' . get_template_directory_uri() .'/img/rose-wine-icon.png" alt="Rosé Wine" width="35" height="35"> Rosé';
}
elseif ($style == 'Sweet') {
echo '<img src="' . get_template_directory_uri() .'/img/sweet-wine-icon.png" alt="Sweet Wine" width="35" height="35"> Sweet';
}
elseif ($new == 'New Wines') {
echo '<img src="' . get_template_directory_uri() .'/img/new-wine-icon.png" alt="New Wine" width="35" height="35">';
}
}
echo '</div>';
}
add_action( 'woocommerce_single_product_summary', 'tutsplus_list_attributes', 0 );
What I am doing wrong? Any help is appreciated.