Home

Awesome

<a href="https://www.magepal.com" title="Magento Extensions" ><img src="https://image.ibb.co/dHBkYH/Magepal_logo.png" width="100" align="right" title="Magento Custom Modules" /></a>

Ajax Newsletter Subscription for Magento 2

Installation

Step 1

Using Composer (recommended)
composer require magepal/magento2-ajax-newsletter-subscribe
Manually

Step 2 - Enable extension ("cd" to {Magento root} folder)

  php -f bin/magento module:enable --clear-static-content MagePal_AjaxNewsletterSubscribe
  php -f bin/magento setup:upgrade

Usage

$('#newsletter-validate-detail').submit(function (e) {
    if ($(this).valid()) {
         var url = $form.attr('action');
         var postData = $form.serializeArray();
    
        try {
            $.ajax({
                url: url,
                dataType: 'json',
                type: 'POST',
                showLoader: true,
                data: $.param(postData),
                complete: function (data) {
                    if (typeof data === 'object') {
                        data = data.responseJSON;
                        //json object
                    } else {
                        //Unknown Error
                    }
                }
            });
        } catch (e) {
                //check for errors
        }
    }
    
    return false;
});

Return JSON


$response = [
    'status' => 1,
    'msg' => __('The confirmation request has been sent.'),
];

$response = [
    'status' => 0,
    'msg' => __('There was a problem with the subscription: %1', $e->getMessage()),
];


© MagePal LLC. | www.magepal.com