Hello everyone,

I have a Joomla website which are using VirtueMart. I want to watch the submit of a product (which is using AJAX) and when the request is succesful and complete then send a new XHR request to upload another part of the website.
How do you think I should do with Mootools?
I first success to do it but only by adding an event on the form submit. So when I click on the Add to Cart button, it is working but I would like to wait until the first request is complete.
Does it make sense?

Thank you for any help you can provide.

Dani AI

Generated

Nice progress by — waiting for the first AJAX call to finish before firing a second request is the right idea. If you can edit the code that creates VirtueMart's Request, add your follow-up call inside that request's completion callback. When that is not possible, two less-invasive, reliable patterns work well: intercept MooTools Request instances globally (filtering by URL or options so you only catch the cart request), or observe the DOM for the cart update and trigger your follow-up when the UI actually changes.

A safe global interceptor that attaches a one-time complete handler to each Request instance (and filters by URL) looks like this:

(function(){
  if (typeof Request === 'undefined' || !Request.prototype.send) return;

  var originalSend = Request.prototype.send;

  Request.prototype.send = function() {
    if (!this._followupHooked) {
      this._followupHooked = true;
      this.addEvent('complete', function() {
        var url = this.options && this.options.url;
        if (url && /addcart|cart|add_to_cart/i.test(url)) {
          // follow-up XHR (adjust URL/data as needed)
          new Request({ url: '/path/to/followup', method: 'post' }).send();
        }
      });
    }
    return originalSend.apply(this, arguments);
  };
})();

If you prefer not to touch MooTools internals, use a MutationObserver on the cart DOM (with a polling fallback for older browsers). Watch a stable selector that changes only after the add-to-cart finishes, then run the follow-up request once. Avoid modifying VirtueMart core files; implement changes as a plugin or template override, filter carefully to prevent duplicate triggers, and prefer success-specific callbacks (onSuccess) if you only want to proceed on HTTP success.

I found two ways for doing this. First it is to use the OnComplete function on XHR object, so you can launch a new function after the request is completed. The other one is by using a timer. The second function will be launch only after a predefined time is spent.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.