Select custom with conflicting script

0
So I have a script to change the default select of WooCommerce variations, so far so good, the problem is when a variation is out of stock, the custom select should have the same options as the default select.

"Select" custom above, default below. White and P are out of stock.

" Select "above, default below. White and P are out of stock.

I found that the WooCommerce JS Variations removes options out of stock programmatically, but as my script runs after the WooCommerce script I thought it should work. I even managed to make it work by adding a 2000ms setTimeout, but it's too long and actually breaks other functions I have. I'll be grateful if anyone knows how to solve this.

$('.variations select').each(function(){
    var select = $(this);
    var div = $('<div class="grupo-atributos">');
    var ul = $('<ul>');
    select.parent('.value').siblings('.label').find('label').each(function(){
      var label = $(this).text();
      div.append('<span>'+label+'</span>');
    });

    $('#custom-select-produto-variavel').append(div);
    div.append(ul);

    select.find('option').each(function(){
      var titulo = $(this).text();
      var data_value = $(this).val();
      ul.append('<li data-value='+data_value+'>'+titulo+'</li>');
    
    select.change(function(){
    select.find('option:selected').each(function(){
      var opcao_selected = $(this);
      select.find('option:not(:selected)').each(function(){
        var opcao_not_selected = $(this);
        $('#custom-select-produto-variavel li').each(function(){
          var opcao_custom = $(this);
          if(opcao_custom.attr('data-value')==opcao_selected.val())
              opcao_custom.addClass('atributo-selected');
          if(opcao_custom.attr('data-value')==opcao_not_selected.val())
              opcao_custom.removeClass('atributo-selected');
        });
      });
    });
    }).change();

    });
  });

  $('#custom-select-produto-variavel div ul li:contains("Escolha uma opção")').remove();

  $('#custom-select-produto-variavel ul li').click(function() {
    var novoVal = $(this).data('value');
    $('.variations select:has([value='+novoVal+'])').val(novoVal);
    $('.variations select').trigger('change');
  });
<div id="custom-select-produto-variavel"></div>
    
asked by anonymous 12.09.2018 / 16:09

1 answer

0

I found that the default select has a delay of 100 ms to start, so to make my script work properly, I needed to add those lines.

$(document).ready(function(){
setTimeout(function(){
 // Meu script
}, 150);
});
    
12.09.2018 / 18:35