0

I'm using Woocommerce (Wordpress) and utilizing the custom event listener defined by woocommerce called show_variation to listen for changes in the variation shown (when changed from viewing one variation of a product to another).

Question 1
The problem with this event listener is that it fires on page load, and I need it to only fire on changes (after page load). As I'm not sure whether this could load after the jQuery $(window).on('load', function(){... I'm reluctant to placing my faith entirely on the on(load) event. Also I would prefer it to only be fired .on('click').

Therefore my question is: is it possible to require a double event for the function to be fired? I'm thinking something like beneath (doesn't work though):

$(element).on('click + show_variation', function(){
alert("variation  shown");
});

As of now I've "solved" this by doing wrapping the show_variation event listener inside a click event listener, but I'm afraid that this isn't a bulletproof solution:

$(container).on('click', function(){
   $(element).on('show_variation', function(){
      alert("variation shown");
   });
});

However, using te last solution causes the show_variation event listener to bubble (adding multiple event listeners and firing multiple functions).

Chri.s
  • 1,386
  • 1
  • 12
  • 23
  • Throw away the first time it triggers, then? You could do that with a simple flag variable... Instead of trying to force it to load differently, modify the way your event watcher handles the event. – random_user_name Dec 21 '17 at 20:19
  • @cale_b I've considered that as well, however the problem is that I'm also using it on the shop page with multple products shown, and here it fires the same number of times as there are variable products on the page (more or less dynamic). – Chri.s Dec 21 '17 at 20:20
  • 1
    Have you checked out this? Maybe a different event does what you want? https://stackoverflow.com/a/27849208/870729 – random_user_name Dec 21 '17 at 20:22
  • @cale_b considering my question that's actually the correct source/answer - however, i'm actually using the function to access the variation data (to get variation id), which is available from the `show_variation` by stating `var variation_id = variation.variation_id;` which needs a `variation` argument in the function which I admittely haven't added in the example in the question :) – Chri.s Dec 21 '17 at 21:03
  • Ah, gotcha. And that other event doesn't pass the variation, or the event target? – random_user_name Dec 21 '17 at 21:09
  • Sadly no - at least not when I pass arguments as such: `$(element).on('woocommerce_variation_select_change',function(event, variation){ ...});`. I guess i'll be sticking to only applying it on `window.load` – Chri.s Dec 21 '17 at 21:11

0 Answers0