• Resolved timholz

    (@timholz)


    Hi – you put the reset button in the foreach loop in variable.php. That does not make sense. One button is enough. Regards Theo

    • This topic was modified 1 year, 5 months ago by timholz.
Viewing 4 replies - 1 through 4 (of 4 total)
  • Thank you for pointing this out, @timholz!

    I’d love to better understand the issue you’re describing. Could you clarify which reset button you’re referring to and how it’s affecting your site? If you have any screenshots or steps to reproduce the issue, that would be helpful as well.

    Let us know so we can assist you further!

    Thread Starter timholz

    (@timholz)

    @mahfuzurwp – thanks for responding. For the variations of a variable product a reset button is generated on the product page. It is underneath the list of variations. The rendering of these variations and the reset button is handled in templates/single-product/add-to-cart/variable.php. In this file a foreach loop cycles through all variations and displays them. In your template the reset button is part of the foreach loop. That means, it is repeatedly evoqued for each variation. Here is the code from variable.php as is (with explanations):

    <table class="variations" role="presentation">
    <tbody>
    <?php foreach ( $attributes as $attribute_name => $options ) : ?>
    <tr>
    <th class="label"><label for="<?php echo esc_attr( sanitize_title( $attribute_name ) ); ?>"><?php echo wc_attribute_label( $attribute_name ); // WPCS: XSS ok. ?></label></th>
    <td class="value">
    <?php
    wc_dropdown_variation_attribute_options(
    array(
    'options' => $options,
    'attribute' => $attribute_name,
    'product' => $product,
    ));
    ?>
    </td>
    </tr>
    <!-- here i added the endforeach command -->
    <tr>
    <td colspan="2">
    <?php
    /**
    * Filters the reset variation button.
    *
    * @since 2.5.0
    *
    * @param string $button The reset variation button HTML.
    */
    echo end( $attribute_keys ) === $attribute_name ? wp_kses_post( apply_filters( 'woocommerce_reset_variations_link', '<button class="reset_variations" aria-label="' . esc_html__( 'Clear options', 'woocommerce' ) . '">' . esc_html__( 'Clear', 'woocommerce' ) . '</button>' ) ) : '';
    ?>
    </td>
    </tr>
    <?php endforeach; //this endforeach comes to late and it repeatedly adds markup to every variation ?>
    </tbody>
    </table>

    Since this code adds additional markup to every variation and since only one reset_variations button is needed, i moved up the endforeach command. See comment in code above. I also do not want to restore the original version for my altered variable.php works well. Therefore i’ll abstain from generating screenshots. I leave it as it is. Regards theo

    Hi @timholz,

    Thank you for sharing your thoughts! I understand your reasoning, but I’d like to clarify how the code works in the default variable.php template:

    While the reset_variations button is located inside the foreach loop, it is intentionally rendered only once. This is controlled by the logic:

    echo end( $attribute_keys ) === $attribute_name ? wp_kses_post( apply_filters( 'woocommerce_reset_variations_link', '' . esc_html__( 'Clear', 'woocommerce' ) . '' ) ) : '';

    This checks if the current attribute being processed is the last one (using end( $attribute_keys )), ensuring the reset button is added only once for the final attribute.

    In the default setup, no additional markup or unnecessary reset buttons are rendered for other attributes, so the functionality remains optimized.

    That said, if your custom approach works better for your specific needs, feel free to keep it as is!

    I hope this helps, let us know if you have any questions!

    Thread Starter timholz

    (@timholz)

    @mahfuzurwp Hi – thanks for the concise answer. All the best for you. Regards Theo

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘variable.php 9.5.0 in latest update’ is closed to new replies.