Reputation: 151
I've edited WooCommerce template so the output of variation labels will be in the same tr and variation values will be in the same tr accordingly.
So far it works fine but i want to change tr after the third td-loop.
This is my php:
<table class="variations" cellspacing="0">
<tbody>
<?php $labels = $values = ''; ?>
<?php $loop = 0; foreach ( $attributes as $attribute_name => $options ) : $loop++; ?>
<?php
$swatches = theme_has_swatches( $product->get_id(), $attribute_name, $options, $available_variations, $swatches_use_variation_images);
$active_variations = theme_get_active_variations( $attribute_name, $available_variations );
?>
<?php ob_start(); ?>
<td class="label" style="padding-right: 15px;min-width: 175px;"><label for="<?php echo esc_attr( sanitize_title( $attribute_name ) ); ?>"><?php echo wc_attribute_label( $attribute_name ); ?></label></td>
<?php $labels .= ob_get_contents(); ob_end_clean(); ?>
<?php ob_start(); ?>
<td class="value <?php if ( ! empty( $swatches ) ): ?>with-swatches<?php endif; ?>" style="padding-right: 15px;min-width: 175px;">
<?php if ( ! empty( $swatches ) ): ?>
<div class="swatches-select swatches-on-single" data-id="<?php echo esc_attr( sanitize_title( $attribute_name ) ); ?>">
<?php
if ( is_array( $options ) ) {
if ( isset( $_REQUEST[ 'attribute_' . $attribute_name ] ) ) {
$selected_value = $_REQUEST[ 'attribute_' . $attribute_name ];
} elseif ( isset( $selected_attributes[ $attribute_name ] ) ) {
$selected_value = $selected_attributes[ $attribute_name ];
} else {
$selected_value = '';
}
// Get terms if this is a taxonomy - ordered
if ( taxonomy_exists( $attribute_name ) ) {
$terms = wc_get_product_terms( $product->get_id(), $attribute_name, array( 'fields' => 'all' ) );
$swatch_size = theme_wc_get_attribute_term( $attribute_name, 'swatch_size' );
$_i = 0;
$options_fliped = array_flip( $options );
foreach ( $terms as $term ) {
if ( ! in_array( $term->slug, $options ) ) {
continue;
}
$key = $options_fliped[$term->slug];
$style = '';
$class = 'theme-swatch swatch-on-single ';
$class .= ' swatch-size-' . $swatch_size;
if ( $selected_value == $term->slug ) {
$class .= ' active-swatch';
}
echo '<div class="' . esc_attr( $class ) . '" data-value="' . esc_attr( $term->slug ) . '" ' . selected( sanitize_title( $selected_value ), sanitize_title( $term->slug ), false ) . ' style="' . esc_attr( $style ) .'">' . apply_filters( 'woocommerce_variation_option_name', $term->name ) . '</div>';
$_i++;
}
} else {
foreach ( $options as $option ) {
$class = '';
if ( $selected_value == $option ) {
$class .= ' active-swatch';
}
if ( 'yes' === get_option( 'woocommerce_hide_out_of_stock_items' ) && $active_variations ) {
if ( in_array( $term->slug, $active_variations ) ) {
$class .= ' swatch-enabled';
} else {
$class .= ' swatch-disabled';
}
}
echo '<div class="' . esc_attr( $class ) . '" data-value="' . esc_attr( sanitize_title( $option ) ) . '" ' . selected( sanitize_title( $selected_value ), sanitize_title( $option ), false ) . '>' . esc_html( apply_filters( 'woocommerce_variation_option_name', $option ) ) . '</div>';
}
}
}
?>
</div>
<?php endif; ?>
<?php
wc_dropdown_variation_attribute_options( array(
'options' => $options,
'attribute' => $attribute_name,
'product' => $product,
) );
echo end( $attribute_keys ) === $attribute_name ? wp_kses_post( apply_filters( 'woocommerce_reset_variations_link', '<a class="reset_variations" href="#">' . esc_html__( 'Clear', 'woocommerce' ) . '</a>' ) ) : '';
?>
</td>
<?php $values .= ob_get_contents(); ob_end_clean(); ?>
<?php endforeach;?>
<?php echo '<tr>', $labels, '</tr><tr>', $values, '</tr>'; ?>
</tbody>
</table>
I've tried to set $_i = 1
and the following statement but it didn't work.
What am i doing wrong?
if ($_i % 3 == 0){
echo "</tr><tr>";
}
$_i++;
Upvotes: 1
Views: 315
Reputation: 21
You must use a division of 3 modulo.
Like this:
if($loop % 3 == 0) {
//if true...
}
Upvotes: 1