Only if multi options, change variations from dropdown to radio buttons
-
Hi,
I want to change the variation’s dropdown to radio buttons only for one option variations, and keep the dropdown for multioption variations.
Changing dropdown to radio buttons code: add_action( 'woocommerce_after_single_product', function() { global $product; if( ! $product || ! $product->is_type( 'variable' ) ) { return; } // Inline jQuery ?> <script> jQuery( document ).ready( function( $ ) { $( ".variations_form" ) .on( "wc_variation_form woocommerce_update_variation_values", function() { $( "label.generatedRadios" ).remove(); $( "table.variations select" ).each( function() { var selName = $( this ).attr( "name" ); $( "select[name=" + selName + "] option" ).each( function() { var option = $( this ); var value = option.attr( "value" ); if( value == "" ) { return; } var label = option.html(); var select = option.parent(); var selected = select.val(); var isSelected = ( selected == value ) ? " checked=\"checked\"" : ""; var selClass = ( selected == value ) ? " selected" : ""; var radHtml = <code><input name="${selName}" type="radio" value="${value}" /></code>; var optionHtml = <code><label class="generatedRadios${selClass}">${radHtml} ${label}</label></code>; select.parent().append( $( optionHtml ).click( function() { select.val( value ).trigger( "change" ); } ) ) } ).parent().hide(); } ); } ); } ); </script> <?php } );
Css to hide radio buttons:
.variations .value .selected input[type=radio]{ opacity: 0; } .variations .value :not(label:only-of-type).selected{ opacity: 1; }
Any help as much appreciated!
Viewing 6 replies - 1 through 6 (of 6 total)
Viewing 6 replies - 1 through 6 (of 6 total)
- The topic ‘Only if multi options, change variations from dropdown to radio buttons’ is closed to new replies.