Woocommerce change from commas to table cells

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.

Related posts

Leave a Reply

2 comments

  1. Basicly you have, with that code, this output:

    <tr>
    <th>atribute label</th>
    <td>atribute name</td>
    <tr>
    

    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:

    <tr class="<?php if ( ( $alt = $alt * -1 ) == 1 ) echo 'alt'; ?>">
    
    
        <td>
            <?php echo $woocommerce->get_helper( 'attribute' )->attribute_label( $attribute['name'] ); ?>
            <?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>
    
  2. 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:

    <?php foreach ( $attributes as $attribute ) :
    
        if ( empty( $attribute['is_visible'] ) || ( $attribute['is_taxonomy'] && ! taxonomy_exists( $attribute['name'] ) ) )
            continue;
        ?>  
        <tr>
            <th scope="row"><?php echo $woocommerce->attribute_label( $attribute['name'] ); ?></th>
            <?php
                if ( $attribute['is_taxonomy'] ) {
    
                    $values = woocommerce_get_product_terms( $product->id, $attribute['name'], 'names' );
                    foreach ( $values as $value ) :
                        echo '<td>'; 
                        echo $value;
                        echo '</td>';
                    endforeach;
                } else {
    
                    $values = array_map( 'trim', explode( '|', $attribute['value'] ) );
                    foreach ( $values as $value ) :
                        echo '<td>'; 
                        echo $value;
                        echo '</td>';
                    endforeach;
                }
            ?>
        </tr><?php endforeach; ?>