How Do I Create a Pop Up Newsletter in Magento 2?

Creating a pop-up newsletter in Magento 2 is relatively simple. First, create a new module in your store and name it “Newsletter.” Next, add the following code to your module’s template file:

Next, add the following code to your module’s style.css file:

newsletter { position: absolute; top: 0; left: 0; width: 100%; height: 100%; background: #fff; } .newsletter h2 { margin: 0; font-size: 20px; font-weight: normal; text-align: center; } .newsletter p { margin: 0 10px 0 0; padding: 10px 20px 10px; } .newsletter .btn { margin: 5px 8px 5px 8px; width: 50%; height: 50%; background:#fff url(./images/btn_subscribe.

png) no-repeat center center; cursor: pointer; } .btn img { max-width: 100%; display: block; }.

Next, add the following code to your module’s functions.php file to create and manage your newsletter subscribers list:

function subscribeForm() { $form = array( ‘#subscription_form’ => array( ‘#type’ => ‘post’, ‘#title’ => t(‘Subscribe’), ‘#required’ => false, ), ); return $form ; } function unsubscribeForm() { $form = array( ‘#unsubscription_form’ => array( ‘#type’ => ‘post’, ‘#title’ => t(‘Unsubscribe’), ‘#required’ => false, ), ); return $form ; } function subscribeView() { return view(‘sample.listing/subscription’); } function unsubscribeView() { return view(‘sample.listing/unsubscription’); } function subscribeFormInner() { if ( ! isset($_POST[‘subscription’])) $_POST[‘subscription’] = array(); // Create a new subscription form $form = subscribeForm(); // Add fields to the subscription form // Add a submit button to the subscription form $form->addField(‘name’, ‘Your Name’); $form->addField(’email’, ‘[email protected]’); // Add a cancel button to the subscription form $form->addField(‘cancel’); // Add a textarea for entering your newsletter content $form->addField(‘message’); // Add a submit button to the subscription form $form->addButton(‘Subscribe’, array($this,’Subscribe’)); // Return the subscription form return $form ; } function unsubscribeFormInner() { if ( ! isset($_POST[‘unsubscription’])) $_POST[‘unsubscription’] = array(); // Unsubscribe from the newsletter if ( ! is_array($_POST[‘unsubscribed’])) $_POST[‘unsubscribed’] = array(); // Create a new unsubscription form $form = unsubscribeForm(); // Add fields to the unsubscription form // Remove the cancel button from the unsubscription form $form->removeField(‘cancel’); // Remove the textarea for entering your newsletter content from the unsubscription form $form->removeField(‘message’); // Remove the submit button from the unsubscription form $form->removeButton(‘Unsubscribe’, array($this,’Unsubscribe’)); // Return the unsubscription form return $form ; } /* * Function that displays and manages a list of subscribers */ function listSubscribeSubscribers() { global $_SESSION[‘userdata’] ; /** * Get subscribers list */ $_SESSION[‘userdata’] = getUserData(); /** * Filter subscribers list by user ID */ if (!empty($_SESSION[‘userdata’][‘id’])) { foreach ($_SESSION[‘userdata’][‘users’] as$user) { if ($user->id == $_SESSION[‘userdata’][‘id’]){ listSubscribeSubscribers($user); break; } } } else listSubscribeSubscribers(“”); /** * Return subscribers list */ return listSubscribeSubscribers; } /* * Function that displays and manages a list of unsubscribers */ function listUnsubscribeSubscribers() { global $_SESSION[‘userdata’] ; /** * Get subscribers list */ $_SESSION[‘userdata’] = getUserData(); /** * Filter subscribers list by user ID */ if (!empty($_SESSION[‘userdata’][‘id’])){ foreach ($_Session[‘userdata’][‘users’] as$user) { if ($user->id != $_SESSION[‘userdata’][‘id’]){ unlistSubscribeSubscribers($user); break; } } } else unlistSubscribeSubscribers(“”); /** * Return subscribers list */ return unlistSubscribeSubscribers; } /* * Function that displays and manages a modal window that lets users subscribe or unsubscribe from the newsletter */ function newsletterModal() { global $_SESSION[]; $_SESSION[=’modal__title__email__message__submit__close__submit_text__’;] ? setTimeout(function(){ Newsletter::listSubscribeSubscribers },1000): Newsletter::listUnsubscribeSubscribERS ();} /* ************************************************************************* */ /* Function that creates and manages an empty subscriber record */ function getUserData() {}.

Related Posts