Last active
March 25, 2024 16:54
-
-
Save Paulsky/eda12eb836f10d4ba8d3cbb6e895faf8 to your computer and use it in GitHub Desktop.
Automatically selects the only available 'Avada Button' type variation for WooCommerce products on the Avada theme.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function(window, document, $) { | |
'use strict'; | |
function handleVariationChange() { | |
$('.variations_form .avada-select-wrapper').each(function() { | |
const $wrapper = $(this); | |
const $buttons = $wrapper.find('.avada-button-select:not([data-disabled="true"])'); | |
const $checkedButtons = $buttons.filter('[data-checked="true"]'); | |
if ($buttons.length === 1 && !$buttons.attr('data-prevent-auto-select')) { | |
$buttons.click(); | |
} else if ($checkedButtons.length > 0 && !$checkedButtons.attr('data-prevent-auto-select')) { | |
$checkedButtons.click(); | |
} | |
}); | |
} | |
function markPreselectedButtons() { | |
$('.avada-button-select[data-checked="true"]').attr('data-prevent-auto-select', 'true'); | |
} | |
$(document).ready(function() { | |
let clickedVariation = false; | |
markPreselectedButtons(); | |
$('.variations_form').on('woocommerce_variation_has_changed', function() { | |
if (clickedVariation) { | |
setTimeout(handleVariationChange, 500); | |
} | |
}); | |
$('.avada-button-select').on('click', function() { | |
clickedVariation = true; | |
const $button = $(this); | |
$button.attr('data-prevent-auto-select', 'true'); | |
$button.closest('.avada-select-wrapper').find('.avada-button-select').not(this).removeAttr('data-prevent-auto-select'); | |
}); | |
}); | |
})(window, document, jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment