1

我想覆盖 Woocommerce 输出属性的方式。默认情况下,属性显示在两列中 - 第 1 列是标签,第 2 列是逗号分隔的属性列表。我想覆盖它并将每个属性显示在自己的单元格中。这是原始代码:

<?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; ?>

我一直在想我需要在其中设置另一个foreach语句,td但我不确定如何设置它。

4

2 回答 2

1

基本上,使用该代码,您将获得以下输出:

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

因此,您只需要输出类似:“属性标签 - 属性名称”的内容,因为您只需要删除 th 标签并将标签代码放入 td 标签内。

像这样:

<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>
于 2013-07-04T00:19:32.207 回答
1

我想通了...我需要调整foreach语句以及单元格在行中的显示方式(以及其中出现的内容),这是我的最终代码,它正在运行,但是,我希望提出改进它的建议:

<?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; ?>
于 2013-07-25T03:37:10.063 回答