Variations radio buttons with swatches

Changes variation drop-down form fields to radio button fields.

If you need a more advanced version of this functionality, check out the official extension WooCommerce Variation Swatches and Photos.

add_action( 'wp_print_footer_scripts', function() {

	// Require Woo
	if( ! class_exists( 'WooCommerce' ) ) {
		return;
	}

	// Single Products Only
	if( ! is_product() ) {
		return;
	}

	?>
	<script type="text/javascript">

	// DOM Loaded
	document.addEventListener( 'DOMContentLoaded', function() {

		// Loop Variations Forms
		document.querySelectorAll( 'form.variations_form' ).forEach( function( variations_form ) {

			// Get Variation Pricing Data
			var data = variations_form.getAttribute( 'data-product_variations' );
			if( data ) {
				data = JSON.parse( data );
			}

			// Loop Drop Downs
			document.querySelectorAll( 'table.variations select' )
				.forEach( function( select ) {

				// Loop Drop Down Options
				select.querySelectorAll( 'option' )
					.forEach( function( option ) {

					// Skip Empty
					if( ! option.value ) {
						return;
					}

					// Create Radio
					let radio = document.createElement( 'input' );
						radio.type = 'radio';
						radio.name = select.name;
						radio.value = option.value;
						radio.checked = option.selected;
						radio.classList.add( 'wc-block-components-radio-control__input' );

					// Radio Label Wrapper
					let label = document.createElement( 'label' );
						label.classList.add( 'wc-block-components-radio-control' );
						label.appendChild( radio );

					// Maybe Include Swatch
					if( data.length ) {
						data.forEach( function( row ) {
							if( row.attributes[select.name] == option.value && row.image ) {
								let thumb = document.createElement( 'img' );
									thumb.alt = row.image.alt ? row.image.alt : '';
									thumb.src = row.image.src ? row.image.src.replace( '.jpg', '-150x150.jpg' ) : '';
									thumb.srcset = row.image.srcset ? row.image.srcset : '';
								label.appendChild( thumb );
							}
						} );
					}

					// Label Text
					label.appendChild( document.createTextNode( ' ' + option.text + ' ' ) );

					// Insert Radio
					select.closest( 'td' ).appendChild( label );

					// Handle Clicking
					radio.addEventListener( 'click', function( event ) {
						select.value = radio.value;
						jQuery( select ).trigger( 'change' );
					} );

				} ); // End Drop Down Options Loop

				// Hide Drop Down
				select.style.display = 'none';

			} ); // End Drop Downs Loop

		} ); // End Variations Forms Loop

	} ); // End Document Loaded

	</script>
	<?php

} );

Recommended CSS

.wc-block-components-radio-control .wc-block-components-radio-control__input {
  padding: 0 !important;
  margin: 0 !important;
  height: auto !important;
  top: 1.2em !important;
  left: 0 !important;
  position: relative !important;
}

Instructions for Variations radio buttons with swatches

  1. Log into a staging or locally hosted clone of your site.
  2. Install and activate Code Snippets plugin.
  3. WP Admin > Snippets > Add New
  4. Copy and paste the code from the section above.
  5. Check to ensure formatting came over properly.
  6. Customize the code as desired.
  7. Add a meaningful title.
  8. Select whether to run on front-end or back-end (or both).
  9. Click “Save and Activate”.
  10. Test your site to ensure it works.
  11. Disable if any problems, or recover.
  12. Repeat for live environment.

Need help modifying Variations radio buttons with swatches?

Contact me. I can help with fitting projects or refer to my partner.

License

All code snippets are licensed GPLv2 (or later) matching WordPress licensing.

Except when otherwise stated in writing the copyright holders and/or other parties provide the program as-is without warranty of any kind, either expressed or implied, including, but not limited to, the implied warranties of merchantability and fitness for a particular purpose.

Disclaimer of warranty