3

In Product's Edit page, when we click Variations, we will see AJAX loading for all of its variations.

Is there a Hook for when it finished?

I want to run a script to add some "at-a-glance" information on each Toggle Header. For example "Sold Out" label when the variation has 0 stock.

Thanks

4
  • It is javascript so you are looking for a callback not a hook as such... you could maybe search the variations javascript to find a callback...
    – majick
    Feb 13 '16 at 4:06
  • @majick Unless I'm mistaken, do I need to edit the WooCommerce source file if I want to add callback in that Javascript? EDIT it seems there's event listener as answered below. so I'm indeed mistaken
    – hrsetyono
    Feb 13 '16 at 4:10
  • yep that looks like the droid - err - i mean callback you are looking for.
    – majick
    Feb 13 '16 at 4:12
  • Do not edit plugin files and or WordPress core files, your changes will be lost in updates, it's bad practice. Always look for hooks, actions/filters or events (if working with JS), there's almost always a way to solve a problem without ever having to resort to editing core files of any plugin. Even at a worse-case scenario, you can use $.ajaxStop() if there was no such event and listen/observe for DOM mutations.
    – Adam
    Feb 13 '16 at 4:13
10

In woocommerce/includes/class-wc-ajax.php there is a method on the WC_AJAX class responsible for loading variations called load_variations, it contains only one hook, which is a filter, named woocommerce_ajax_admin_get_variations_args which fires early in the method.

However if you are looking for something client side, then woocommerce/assets/js/admin/meta-boxes-product-variation.js triggers an event on the success callback of the load_variations function named woocommerce_variations_loaded.

Therefore if you are looking to fire subsequent actions using JS, then:

$(document).on('woocommerce_variations_loaded', function(event) {
    //your code here...
});

Or you can try binding to the element in which the trigger is executed upon:

$('#woocommerce-product-data').on('woocommerce_variations_loaded', function(event) {
    //your code here...
});
1
  • 2
    Thanks, the second one ($("#woocommerce-product-data")...) works great! I wish WooCommerce is better documented :(
    – hrsetyono
    Feb 13 '16 at 4:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.