Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kirillrocks/c866ed0eb9d20f50cb6391356b3ee5ab to your computer and use it in GitHub Desktop.
Save kirillrocks/c866ed0eb9d20f50cb6391356b3ee5ab to your computer and use it in GitHub Desktop.
(function($) {
$(document).ready(function() {
$("select[name='attribute_size']").on("change", function(){
var selected_size = $(this).val();
$("select[name='attribute_size']").val(selected_size);
activateSelect(selected_size);
});
function activateSelect(selected_size){
var woos = $(".single-product");
for (j=0; j<woos.length; j++) {
$(woos[j]).find(".woocommerce-variation").addClass("show-price");
$(woos[j]).find(".woocommerce-variation-add-to-cart").removeClass("woocommerce-variation-add-to-cart-disabled");
$(woos[j]).find(".woocommerce-variation-add-to-cart").addClass("woocommerce-variation-add-to-cart-enabled");
$(woos[j]).find(".single_add_to_cart_button").removeClass("disabled");
$(woos[j]).find(".single_add_to_cart_button").removeClass("wc-variation-selection-needed");
var formdata = $(woos[j]).find(".variations_form").data("product_variations");
var chosenelement;
for (i=0; i<formdata.length; i ) {
if(formdata[i].attributes.attribute_size == selected_size) {
chosenelement = formdata[i];
}
}
$(woos[j]).find(".woocommerce-variation").html(`
<div class="woocommerce-variation-description">${chosenelement.variation_description}</div>
<div class="woocommerce-variation-price">${chosenelement.price_html}</div>
<div class="woocommerce-variation-availability">${chosenelement.availability_html}</div>
`);
$(woos[j]).find(".woocommerce-variation-add-to-cart input[name='variation_id']").attr({"value": chosenelement.variation_id});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment