This is not a Woocommerce-specific question! This is a question about the loop in the included code.
In working with Woocommerce's product-attributes.php template, there is a loop that cycles through attributes, displaying them, along with their terms.
In the attached image you can see the table that is output. variable single product page
You can check out the code in question at the bottom of this post.
My problem is I only want the term of the current variable product selected to be displayed after its attribute label. As you can see in the image, I have a 'dimension' selected, yet all of the terms of an associated attribute are looped through and displayed in the table (ie, the product should only display a single weight (term)). How do I select the 1 term that is stored with the variable product's attribute? I feel like the array query in the following line $attribute_values = wc_get_product_terms( $product->get_id(), $attribute->get_name(), array( 'fields' => 'all' ) );
is key but I'm not sure how it works.
Research
I've searched seemingly everywhere on Woocommerce's forums, here on Stackoverflow, Google. I've been through the files and isolated the issue to this one template. I'm at wit's end. Can anyone help?
<?php foreach ( $attributes as $attribute ) : ?>
<tr>
<th><?php echo wc_attribute_label( $attribute->get_name() ); ?></th>
<td><?php
$values = array();
if ( $attribute->is_taxonomy() ) {
$attribute_taxonomy = $attribute->get_taxonomy_object();
$attribute_values = wc_get_product_terms( $product->get_id(), $attribute->get_name(), array( 'fields' => 'all' ) );
foreach ( $attribute_values as $attribute_value ) {
$value_name = esc_html( $attribute_value->name );
if ( $attribute_taxonomy->attribute_public ) {
$values[] = '<a href="' . esc_url( get_term_link( $attribute_value->term_id, $attribute->get_name() ) ) . '" rel="tag">' . $value_name . '</a>';
} else {
$values[] = $value_name;
}
}
} else {
$values = $attribute->get_options();
foreach ( $values as &$value ) {
$value = make_clickable( esc_html( $value ) );
}
}
echo apply_filters( 'woocommerce_attribute', wpautop( wptexturize( implode( ', ', $values ) ) ), $attribute, $values );
?></td>
</tr>
<?php endforeach; ?>