D
D
Delerom2021-11-10 17:16:34
PHP
Delerom, 2021-11-10 17:16:34

Change the display order of attributes in a template?

The template has an output of the first 4 attributes. How can I change the loop so that the 3rd attribute is displayed first?

<?php foreach ($product_attributes as $attribute):
            $attribute_data = $attribute->get_data();
            ?>
            <tr class="woocommerce-product-attributes-item woocommerce-product-attributes-item--<?php echo esc_attr($attribute_data['name']); ?>">
                <th class="woocommerce-product-attributes-item__label"><?php echo wc_attribute_label($attribute_data['name']); ?></th>
                <td class="woocommerce-product-attributes-item__value"><?php echo $product->get_attribute($attribute_data['name']); ?></td>
            </tr>
            <?php
            $i += 1;
            if ($i == 4) break;
        endforeach; ?>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
WP Panda, 2021-11-10
@wppanda5

As Anton Litvinenko said before the cycle, change the order of the elements.
But you can stupidly in the forehead.

$out = '';
$html = <<<HTML
  <tr class="woocommerce-product-attributes-item woocommerce-product-attributes-item--%s">
    <th class="woocommerce-product-attributes-item__label">%s</th>
    <td class="woocommerce-product-attributes-item__value">%s</td>
  </tr>
HTML;

$i = 1;
foreach ( $product_attributes as $attribute ):
  $attribute_data = $attribute->get_data();

  if ( $i !== 3 ) {
    $out .= sprintf( $html, esc_attr( $attribute_data['name'] ), wc_attribute_label( $attribute_data['name'], $product->get_attribute( $attribute_data['name'] ) ) );
  } else {
    printf( $html, esc_attr( $attribute_data['name'] ), wc_attribute_label( $attribute_data['name'], $product->get_attribute( $attribute_data['name'] ) ) );
  }

  $i ++;
endforeach;
echo $out;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question