8manos / wc-variations-radio-buttons

Let your customers choose product variations using radio buttons instead of dropdowns.
https://wordpress.org/plugins/wc-variations-radio-buttons/
GNU General Public License v2.0
62 stars 34 forks source link

Enhacement to work in archive pages #25

Closed NatachaMenjibar closed 7 years ago

NatachaMenjibar commented 7 years ago

Hi,

I'm using your plugin for displaying variations in archive pages in conjunction with "Woocommerce Add to cart Ajax for variable products" plugin.

I've found a problem when selecting variable atributes in archive pages for different products with same atributes because inputs of different products has the same name in all products, and the javascript wasn't working well (was always targeting the input of the first product).

I solved it adding some customization in your variable.php, in print_attribute_radio function, adding product ids to inputs id names. That way, javascript can properly target the product imputs from every product. Maybe you can update your plugin with a similar modification.

This is my modification (with comments) in variable.php, line 17:

if ( ! function_exists( 'print_attribute_radio' ) ) {
    function print_attribute_radio( $checked_value, $value, $label, $name ) {
        /* added global product variable to extract product id */
        global $product;
        // This handles < 2.4.0 bw compatibility where text attributes were not sanitized.
        $checked = sanitize_title( $checked_value ) === $checked_value ? checked( $checked_value, sanitize_title( $value ), false ) : checked( $checked_value, $value, false );

        $input_name = 'attribute_' . esc_attr( $name ) ;
        $esc_value = esc_attr( $value );
        $id = esc_attr( $name . '_v_' . $value . $product->id ); //added product ID at the end of the name to target single products
        $filtered_label = apply_filters( 'woocommerce_variation_option_name', $label );
        printf( '<div class="%1$s %3$s"><input type="radio" name="%1$s" value="%2$s" id="%3$s" %4$s><label for="%3$s">%5$s</label></div>',$input_name, $esc_value, $id, $checked, $filtered_label );
    }
}

Cheers

Natacha

Mantish commented 7 years ago

Hi, looks good would you like to do a pull request with your changes?

NatachaMenjibar commented 7 years ago

Done! i'm github newbie, I hope I do it well :)

Mantish commented 7 years ago

@kelpgit thanks, it's perfect. I just left a comment for a small change