There is a new way of subscribing users based on input fields from the website:
You need to add a click event listener on the submit button of the entire form (which includes the input email field).
We’ve introduced a javascript method you can use to listen to those fields only, and also take into consideration any checkbox from the form to be checked in order to subscribe the user:
checkAndSubscribe (<input email field CSS selector>, <checkbox input CSS selector>, true/false, params)
- first param represents a valid CSS selector for the email input field.
- second param represents a valid CSS selector for the checkbox (user needs to check it in order to be subscribed). If you don’t have one and want to subscribe all users, just set this to false or ” (empty string).
- this param that is either true or false. Set it to false when your form doesn’t redirect to another page (form submit is done through ajax. Set it to true when your form is redirecting to another page (or page reload)
- last param is a JS object to send extra values along with the login object. Keys of this JS object must be user attributes: source, tags or list attribute you want user to subscribe to.
- if there is no list specific attribute to pass subscribe we add subscription to your main list.
Example:
jQuery('#wpmc-next').on('click', function(){ _vtsdk.Helpers.checkAndSubscribe('#billing_email', '#mailchimp_woocommerce_newsletter', true, {source: "top", "_ls_main": true}); });
Or if you want to add listener to the change of checkbox input:
jQuery('#checkoutform [name="agreeNewsletterInformation"]').on('change', function(){
_vtsdk.Helpers.checkAndSubscribe('#checkoutform .storage[name=email]', '#checkoutform [name="agreeNewsletterInformation"]', false, {source: "checkout"});
});