2

I have a plugin that has an onBeforeSaveUser event. This fires the mailchimpsubscribe plugin to subscribe users to selected newsletters.

Everything works correctly the user is subscribed and then the saveUser function saves the users newsletter preferences to the database.

I am now trying to get it to work with an ajax call, which again works correctly and subscribes the user to mailchimp. However after sending the json response it does not appear that the saveUser function is being fired as nothing is updated in the database for the selections made.

Could the ajax be stopping the saveUser from being fired?

Ajax:

// Get the post data
var data = $(this).serialize();

// Send it to the server
$.post('/', data, function (response) {

    if (response.success) {
      $form.parents('.question-box').fadeOut(function () {
        $(this).remove();
        $('.question-box').first().fadeIn();
      });
    } else {
        var message = response.message;
      alert('Unfortunately, we were unable to save your data at this point. Please try again later.'+message);
    }
});

Mailchimp Plugin:

if ($api->errorCode) { // an api error occured 

      // Respond appropriately to Ajax Requests.....

} else { // list subscribe was successful

      // Respond appropriately to Ajax Requests
      if (craft()->request->isAjaxRequest())
      {
        return $this->returnJson(array(
          'success' => true,
          'errorCode' => 1,
          'values' => array(
            'email' => $email,
            'vars' => $vars
          )
        ));
      }

      //..... 
}

Cheers

  • Could you add relevant code samples (PHP/JS) to your question? Might make it easier to pin down the issue. – Mats Mikkel Rummelhoff Jun 10 '15 at 9:41
  • I had an issue in Craft where events weren't triggered when deleting entries from the entry listing page (using the checkboxes). P&T said this was a bug in Craft they are aware of. Maybe this is related in the sense events aren't being triggered for AJAX requests? – Alec Ritson Jun 10 '15 at 9:49
  • @mmikkel code added. As I mentioned everything works as expected until it's submitted with ajax? – Lettie Jun 10 '15 at 9:56
  • @AlecRitson the event is being triggered it's the saverUser after the event that isn't but only when the form is submitted by ajax. Cheers – Lettie Jun 10 '15 at 9:58
3

Had a response from Pixel & Tonic on this. Which explains the process.


The problem you're seeing is caused by returnJson() as that also ends the process after outputting the JSON data. Most likely onSaveUser (without Before) is a better place for this code; that ensures the user has been saved to the database before running the event code.


| improve this answer | |
  • Lettie could you share your plugin please? I need to do exactly the same thing: Add a user to a mailchimp list after saving them. Thank you in advance. – Adam Menczykowski Feb 24 '16 at 11:42
  • @adamMenczykowski it's not actually a separate plugin. I had to customise 2 other plugins that I was using to get it working. alwaysValidate and MailchimpSubscribe. So it would require a bit of coding to get it to work for you. Files are a little messy with loads of comments. But happy to ping the relevant bits to you if you think you can make use of it? – Lettie Feb 24 '16 at 16:29
  • That would be amazing, as a lesson in what you did and to help me out - I would greatly appreciate it. If you could email adammenczykowski@gmail.com please – Adam Menczykowski Feb 24 '16 at 20:32

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.