Trigger check_variations event in Woocommerce

I'm trying to make a text based selection for my product variationon on my single product page. I basically generate a p-tag for every option in every variation and use javascript to select the option in the default Woocommerce select dropdown. The option gets selected fine but the check_variations event doesn't get triggered.

Does anyone know how to trigger the check_variations event from my theme? The check_variations listener is in woocommerce/assets/js/frontend/add-to-cart-variation.js

JS

var ProductVariations = (function () {
    function ProductVariations() {
        this.$variationClickables = $('.variations .value p');
        this.setupClickHandlers();
    }
    ProductVariations.prototype.setupClickHandlers = function () {
        var _this = this;
        this.$variationClickables.bind('click', function (event) {
            _this.variationsClicked(event);
        });
    };

    ProductVariations.prototype.variationsClicked = function (event) {
        var $target = $(event.target);
        var targetVariation = $target.attr('value');
        $('option[value=' + targetVariation + ']', $target.closest('.variations')).attr('selected', 'true');
        $target.closest('.variations_form').trigger('change');
    };
    return ProductVariations;
})();

Upvotes: 5

Views: 2835

Answers (2)

Nemai Debnath
Nemai Debnath

Reputation: 1

Product variations on single product page can be triggerred by the 'check_variations' event like this below:

$('body').on('check_variations', function(){ 
  $('div.custom_option').removeClass('is-visible');
  $('.variations_form table.variations select').each(function(){
      var attrID = $(this).attr("id");
      $(this).find('option').each(function(){ 
          $('div[data-parent-id="'+attrID+'"][data-value="'+$(this).val()+'"]').addClass('is-visible');
      });
  });
}); 

Upvotes: 0

Artem Lapkin
Artem Lapkin

Reputation: 21

Andreas! Did you try this?

$('.variations_form').trigger('check_variations');

Upvotes: 2

Related Questions