I have a script in jQuery, which basically filters the variations of Woocomerce Wordpress products and selects the desired option by adding the "selected" attribute, the big problem is, I have a button to select the variation, as you can see in I have tried several ways, but some bugs are coming up, what I needed is that just clicking on the "SELECT" item would be selected, here it is the link if you want to take a look at how it works:
Thank you in advance for your help!
;(function ( $ ) {
'use strict';
/**
* @TODO Code a function the calculate available combination instead of use WC hooks
*/
$.fn.tawcvs_variation_swatches_form = function () {
return this.each( function() {
var $form = $( this ),
$vselect = $('.variation-select'),
clicked = null,
selected = [];
$form.addClass( 'swatches-support' )
$vselect.on( 'click', '.selecionar, .selecionado', function ( e ) {
e.preventDefault();
e.stopPropagation(); // para nao haver bubbling do evento para os pais
var $el = $( this ).parents( '.swatch' ),
$select = $el.closest( '.value' ).find( 'select' ),
attribute_name = $select.data( 'attribute_name' ) || $select.attr( 'name' ),
value = $el.data( 'value' );
$select.trigger( 'focusin' );
// Check if this combination is available
if ( ! $select.find( 'option[value="' + value + '"]' ).length ) {
$el.siblings( '.swatch' ).removeClass( 'selected' );
$select.val( '' ).change();
$form.trigger( 'tawcvs_no_matching_variations', [$el] );
return;
}
clicked = attribute_name;
if ( selected.indexOf( attribute_name ) === -1 ) {
selected.push(attribute_name);
}
if ( $el.hasClass( 'selected' ) && e.currentTarget.className == "selecionado" ) {
$select.val( '' );
$el.removeClass( 'selected' );
delete selected[selected.indexOf(attribute_name)];
} else {
$el.addClass( 'selected' ).siblings( '.selected' ).removeClass( 'selected' );
$select.val( value );
}
$select.change();
} )
.on( 'click', '.reset_variations', function () {
$( this ).closest( '.variations_form' ).find( '.swatch.selected' ).removeClass( 'selected' );
selected = [];
} )
.on( 'tawcvs_no_matching_variations', function() {
window.alert( wc_add_to_cart_variation_params.i18n_no_matching_variations_text );
} );
} );
};
$( function () {
$( '.variations_form' ).tawcvs_variation_swatches_form();
$( document.body ).trigger( 'tawcvs_initialized' );
} );
})( jQuery );
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><tdclass="value">
<div class="variation-selector variation-select-image hidden">
<select id="pa_opcoes-de-acabamento" class="" name="attribute_pa_opcoes-de-acabamento" data-attribute_name="attribute_pa_opcoes-de-acabamento" data-show_option_none="yes">
<option value="">Escolha uma opção</option>
<option value="spe-2702-tba-pi" class="attached enabled">SPE-2702-TBA-PI</option>
<option value="spe-2702-tba-po" class="attached enabled">SPE-2702-TBA-PO</option>
<option value="spe-2702-tbi" class="attached enabled">SPE-2702-TBI</option>
<option value="spe-2702-tbm" class="attached enabled">SPE-2702-TBM</option>
<option value="spe-2702q-cz" class="attached enabled">SPE-2702Q-CZ</option>
<option value="spe-2702q-pr" class="attached enabled">SPE-2702Q-PR</option>
<option value="spe-2702r-cz" class="attached enabled">SPE-2702R-CZ</option>
<option value="spe-2702r-pr" class="attached enabled">SPE-2702R-PR</option>
</select>
</div>
<div class="tawcvs-swatches" data-attribute_name="attribute_pa_opcoes-de-acabamento">
<span class="swatch swatch-image swatch-spe-2702-tba-pi" title="SPE-2702-TBA-PI" data-value="spe-2702-tba-pi">
<img class="hs-rsp-popup variation-img" src="https://www.agenciagoup.com.br/projetos/sperone/wp-content/uploads/2018/01/SPE-2702-TBA-PI-600x600.jpg"><spanclass="middle">
<span class="variation-desc">
Dimensões: 190X190mm
Material: Alumínio Polido
Tipo: QUADRADA
Cor: Aluminio polido
</span>
<span class="variation-selected">SELECIONADO</span>
<p class="variation-select ">
<img class="var-slc-img" src="https://www.agenciagoup.com.br/projetos/sperone/wp-content/themes/sperone/img/selecionar.png"><aclass="selecionar">Selecionar</a>
<a class="selecionado">Selecionado</a>
</p>
<a href="https://www.agenciagoup.com.br/projetos/sperone/wp-content/uploads/2018/01/SPE-2702-TBA-PI-600x600.jpg" class="hs-rsp-popup variation-ampliar">
<img class="var-slc-img" src="https://www.agenciagoup.com.br/projetos/sperone/wp-content/themes/sperone/img/lupav.png">Ampliar<imgclass="hs-rsp-popup" src="https://www.agenciagoup.com.br/projetos/sperone/wp-content/uploads/2018/01/SPE-2702-TBA-PI-600x600.jpg">
</a>
</span>
</span>