mailchimp-subscribe-craft
mailchimp-subscribe-craft copied to clipboard
[Feature request] Create a template variable to access SubscribeResponse
There are times when the mailchimpSubscribe
global variable is not accessible, or contains incorrect values (eg. when caching the page). In those cases, it would be great to have an easily accessible template variable to check the correct response status inside a nocache block (can be called something like craft.mailchimpSubscribe.subscribeResponse
).
I have used this plugin inside a Sprig component and I has some problems because after a successful subscription, the mailchimpSubscribe was not accessible.
A template variable would be a great addition!
hi, any chance this works with sprig already?
Got to the bottom of this (re sprig) but would need a plugin update to resolve I think.
Not sure if this is a sprig thing or a Mailchimp Subscribe thing but a redirect parameter is being assigned by sprig on the submission, even if empty or forced eg s-val:redirect=""
.
This means in essence it can never complete because the response is being treated as a redirectToPostedUrl()
rather than passing back with the relevant variables/parameters.
I imagine that this needs to be set to following or similar:
return $this->redirectToPostedUrl(['mailchimpSubscribe' => $result]);
@bencroker do you know if this is a problem that can be fixed at the Sprig level?
@cole007 Why can't the redirect parameter be set to a blank string?
i have done but for some reason it is still being passed through (or at least interpreted) as a hashed string by the time it hits the plugin controller.
Get to the bottom of that and you should be able to solve it!
Mailchimp Subscribe does not redirect if the posted redirect param is an empty string.
This is why I am confused as to where the redirect variable is coming from (when sent via Sprig) - if there is no redirect specified or a redirect of ""
is specified I can see in the plugin controller that it gets a redirect value.
So for example in my sprig form I have added the following parameter:
s-val:redirect=""
Then if in the plugin controller I insert the following:
// if a redirect variable was passed, do redirect
if ($redirect !== '' && $result['success']) {
Craft::dd($redirect);
return $this->redirectToPostedUrl();
}
The following is returned:
ce1ef4f0d86c8a8fec5f746393be0c5dfb3f48fe6e962501b5b09b8eae5cb5cbhttps://{id}'
Even though if I inspect the XHR submission the value for redirect is empty.
Not sure if this is happening at a sprig level or a Craft level or in this plugin 🤷♂️
Looks like this issue was already raised on putyourlightson/craft-sprig-core: https://github.com/putyourlightson/craft-sprig-core/issues/5
Relevant code here: https://github.com/putyourlightson/craft-sprig-core/blob/0af3e30d726e60c97a722255ce66b3ac98abab24/src/controllers/ComponentsController.php#L83
Thanks @aelvan, so we're back to square one, that the plugin should ideally return the result as part of the redirect, as is the convention with Craft controllers.
return $this->redirectToPostedUrl(['mailchimpSubscribe' => $result]);
@bencroker Hm, I don't think that would do anything. An $object
passed to redirectToPostedUrl()
is only used by Craft in parsing the 'redirect'
POST parameter (if there is one) for properties before doing the redirect – it doesn't set the data as route params or anything like that.
I guess a quick fix on MailChimp Subscribe's end could be to make sure that the mailchimpSubscribe
route param is set before that conditional redirect. However, since the plugin is retired and this isn't a regression, I'm not sure if it's worth the effort at this point? FWIW I also think the real issue is how Sprig sets a commonly used POST parameter like that. If technically feasible, perhaps it could use a different parameter name, as suggested by https://github.com/putyourlightson/craft-sprig-core/issues/5#issue-1100588423?
I think you're right @aelvan! What even is the issue @cole007? Is this check provided in https://github.com/putyourlightson/craft-sprig-core/issues/5#issuecomment-1012157446 not sufficient?
{% if success is defined and success %}
Subscribed successfully!
{% else %}
Do something with the `mailchimpSubscribe` variable.
{% endif %}
Thanks - that partly seems to work (sorry, was expecting these to be accessible via the {{ mailchimpSubscribe }}
object, not via the specific attribute.
One oddity though on testing is that success
seems to work on a successfully completed form but if the form fails then the {{ mailchimpSubscribe }}
object is returned.
This should be enough to get me going though, thanks both.
even if I hit an error the response is showing as successful. Eg if wrong value sent through to a pre-defined field (i.e. a dropdown list)
Yes, given how the plugin controller works, that is the expected behaviour.