【问题标题】:Insert <tr> in a table after every third loop每第三个循环后在表中插入 <tr>
【发布时间】:2020-03-25 03:24:55
【问题描述】:

我已经编辑了 WooCommerce 模板,因此变体标签的输出将位于相同的 tr 中,并且变体值将相应地位于相同的 tr 中。

到目前为止它工作正常,但我想在第三个 td-loop 之后更改 tr。

这是我的 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 = 1 和以下语句,但没有成功。 我做错了什么?

if ($_i % 3 == 0){
   echo "</tr><tr>";
}
$_i++;

【问题讨论】:

  • 提及您正在修改的模板文件 (variable.php) 会很有用。还有您更改了哪些代码行:(34 - 54) 您是否尝试过调试或打印 $_i?所以你可以看看这在循环期间是否真的有效?
  • 是的,你是对的。基于这篇文章damiencarbery.com/2017/11/change-woocommerce-variations-layout,我通过使用ob_start 函数并添加以下代码&lt;?php $labels = $values = ''; ?&gt;&lt;?php echo '&lt;tr&gt;', $labels, '&lt;/tr&gt;&lt;tr&gt;', $values, '&lt;/tr&gt;'; ?&gt; 编辑了WooCommerce 的variable.php 文件。

标签: php woocommerce html-table


【解决方案1】:

您必须使用 3 模除法。

像这样:

  if($loop % 3 == 0) {
     //if true...
  }

【讨论】:

  • 使用“==”不是你应该做的;)
  • 为什么不呢?是的,他可以使用“===”。 @maio290
  • @AleksiVirtanen 你能编辑关于我应该如何编辑我的 php 代码的答案吗?它似乎不起作用。
猜你喜欢
  • 2012-02-18
  • 1970-01-01
  • 1970-01-01
  • 2012-10-20
  • 1970-01-01
  • 1970-01-01
  • 2011-06-30
  • 2015-12-24
  • 1970-01-01
相关资源
最近更新 更多