- Create di.xml at app/code/Magemonkeys/AjaxNewsletter/etc/di.xml folder.
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <type name="MagentoNewsletterControllerSubscriberNewAction"> <plugin name="Newsletter_Subscriber_NewAction" type="MagemonkeysAjaxNewsletterControllerPluginSubscriberNewAction" sortOrder="10" disabled="false" /> </type> </config>
- create NewAction.php at app/code/Magemonkeys/AjaxNewsletter/Controller/Plugin/Subscriber folder
<?php /** * */ namespace MagemonkeysAjaxNewsletterControllerPluginSubscriber; use MagentoCustomerApiAccountManagementInterface as CustomerAccountManagement; use MagentoCustomerModelSession; use MagentoCustomerModelUrl as CustomerUrl; use MagentoFrameworkAppActionContext; use MagentoStoreModelStoreManagerInterface; use MagentoNewsletterModelSubscriberFactory; /** * Class NewAction */ class NewAction extends MagentoNewsletterControllerSubscriberNewAction { /** * @var CustomerAccountManagement */ protected $customerAccountManagement; protected $resultJsonFactory; /** * Initialize dependencies. * * @param Context $context * @param SubscriberFactory $subscriberFactory * @param Session $customerSession * @param StoreManagerInterface $storeManager * @param CustomerUrl $customerUrl * @param CustomerAccountManagement $customerAccountManagement */ public function __construct( Context $context, SubscriberFactory $subscriberFactory, Session $customerSession, StoreManagerInterface $storeManager, CustomerUrl $customerUrl, CustomerAccountManagement $customerAccountManagement, MagentoFrameworkControllerResultJsonFactory $resultJsonFactory ) { $this->customerAccountManagement = $customerAccountManagement; $this->resultJsonFactory = $resultJsonFactory; parent::__construct( $context, $subscriberFactory, $customerSession, $storeManager, $customerUrl, $customerAccountManagement ); } /** * Retrieve available Order fields list * * @return array */ public function aroundExecute($subject, $procede) { $response = []; if ($this->getRequest()->isPost() && $this->getRequest()->getPost('email')) { $email = (string)$this->getRequest()->getPost('email'); try { $this->validateEmailFormat($email); $this->validateGuestSubscription(); $this->validateEmailAvailable($email); $status = $this->_subscriberFactory->create()->subscribe($email); if ($status == MagentoNewsletterModelSubscriber::STATUS_NOT_ACTIVE) { $response = [ 'status' => 'OK', 'msg' => 'The confirmation request has been sent.', ]; } else { $response = [ 'status' => 'OK', 'msg' => 'Thank you for your subscription.', ]; } } catch (MagentoFrameworkExceptionLocalizedException $e) { $response = [ 'status' => 'ERROR', 'msg' => __('There was a problem with the subscription: %1', $e->getMessage()), ]; } catch (Exception $e) { $response = [ 'status' => 'ERROR', 'msg' => __('Something went wrong with the subscription.'), ]; } } return $this->resultJsonFactory->create()->setData($response); } }
- Make a copy of “vendor/magento/module-newsletter/view/frontend/templates/subscribe.phtml” and place it inside your Magento Theme (e.g. app/design/frontend/NAMESPACE/MY_CUSTOM_THEME/Magento_Newsletter/templates/subscribe.phtml
Add the following javascript to the end of the subscribe.phtml:{ "*": { "js/newsletter_subscriber_ajax": { } } }
- Create newsletter_subscriber_ajax.js at app/design/frontend/NAMESPACE/MY_CUSTOM_THEME/web/js/ folder
require.config({ deps: [ 'jquery' ], callback: function ($) { var form = $('form.subscribe'); form.submit(function(e) { if(form.validation('isValid')){ var email = $("#newsletter").val(); var url = form.attr('action'); var loadingMessage = $('#loading-message'); if(loadingMessage.length == 0) { form.find('.control').append('<div id="loading-message" style="display:none;padding-top:10px;"> </div>'); var loadingMessage = $('#loading-message'); } e.preventDefault(); try{ loadingMessage.html('Submitting...').show(); $.ajax({ url: url, dataType: 'json', type: 'POST', data: {email: email}, success: function (data){ if(data.status != "ERROR"){ $('#newsletter').val(''); } loadingMessage.html(data.msg); }, complete: function(){ setTimeout(function(){ loadingMessage.hide(); },5000); } }); } catch (e){ loadingMessage.html(e.message); } } return false; }); } })