I want to override the way Woocommerce outputs attributes. By default, the attributes are displayed in two columns – column 1 is a label, column 2 is a comma separated list of attributes. I want to override that and have each attribute display in its own cell. Here’s the original code:
<?php foreach ( $attributes as $attribute ) :
if ( empty( $attribute['is_visible'] ) || ( $attribute['is_taxonomy'] && ! taxonomy_exists( $attribute['name'] ) ) )
continue;
?>
<tr class="<?php if ( ( $alt = $alt * -1 ) == 1 ) echo 'alt'; ?>">
<th><?php echo $woocommerce->get_helper( 'attribute' )->attribute_label( $attribute['name'] ); ?></th>
<td><?php
if ( $attribute['is_taxonomy'] ) {
$values = woocommerce_get_product_terms( $product->id, $attribute['name'], 'names' );
echo apply_filters( 'woocommerce_attribute', wpautop( wptexturize( implode( ', ', $values ) ) ), $attribute, $values );
} else {
// Convert pipes to commas and display values
$values = array_map( 'trim', explode( '|', $attribute['value'] ) );
echo apply_filters( 'woocommerce_attribute', wpautop( wptexturize( implode( ', ', $values ) ) ), $attribute, $values );
}
?></td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
I keep thinking I need to setup another foreach
statement within the td
but I’m not sure on how to get it setup.
Basicly you have, with that code, this output:
So, you only need to output that something like: “atribute label – atribute name”, for that you only need to remove the th tags and put the code for label inside the td ones.
Someting like this:
I figured it out…I needed to adjust the the
foreach
statement and how the cells appear in the row (and what appears in them), here’s my finalized code which is working, however, I would love suggestions to improve it: