Let’s initiate a discussion!!
The newsletter subscription module in Magento has only one field (email) by default. If you want to add an extra field to the form (like the name), perform the following steps:
Magemonkeys/Newsletter/registration.php
1 2 3 4 5 6 |
<?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'Magemonkeys_Newsletter', __DIR__ ); |
Magemonkeys/Newsletter/etc/module.xml
1 2 3 4 5 6 7 |
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> <module name="Magemonkeys_Newsletter" setup_version="1.0.0"/> <sequence> <module name="Magento_Newsletter" /> </sequence> </config> |
Magemonkeys/Newsletter/Setup/InstallSchema.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
<?php namespace Magemonkeys\Newsletter\Setup; use Magento\Framework\DB\Ddl\Table; use Magento\Framework\Setup\InstallSchemaInterface; use Magento\Framework\Setup\ModuleContextInterface; use Magento\Framework\Setup\SchemaSetupInterface; class InstallSchema implements InstallSchemaInterface { public function install(SchemaSetupInterface $setup, ModuleContextInterface $context) { $setup->startSetup(); $table = $setup->getTable('newsletter_subscriber'); $setup->getConnection()->addColumn( $table, 'full_name', [ 'type' => Table::TYPE_TEXT, 'nullable' => true, 'comment' => 'Name', ] ); $setup->endSetup(); } } |
Magemonkeys/Newsletter/etc/di.xml
1 2 3 4 |
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd"> <preference for="Magento\Newsletter\Controller\Subscriber\NewAction" type="Magemonkeys\Newsletter\Controller\Subscriber\NewAction" /> </config> |
Magemonkeys/Newsletter/Controller/Subscriber/NewAction.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
<?php namespace Magemonkeys\Newsletter\Controller\Subscriber; use Magento\Customer\Api\AccountManagementInterface as CustomerAccountManagement; use Magento\Customer\Model\Session; use Magento\Customer\Model\Url as CustomerUrl; use Magento\Framework\App\Action\Context; use Magento\Framework\Controller\ResultFactory; use Magento\Framework\Controller\Result\Redirect; use Magento\Framework\Exception\LocalizedException; use Magento\Framework\Phrase; use Magento\Newsletter\Controller\Subscriber\NewAction as SubscriberNewController; use Magento\Newsletter\Model\Subscriber; use Magento\Newsletter\Model\SubscriberFactory; use Magento\Newsletter\Model\SubscriptionManagerInterface; use Magento\Store\Model\StoreManagerInterface; /** * New newsletter subscription action * * @SuppressWarnings(PHPMD.CouplingBetweenObjects) */ class NewAction extends SubscriberNewController { /** * @var SubscriptionManagerInterface */ private $subscriptionManager; /** * Initialize dependencies. * * @param Context $context * @param SubscriberFactory $subscriberFactory * @param Session $customerSession * @param StoreManagerInterface $storeManager * @param CustomerUrl $customerUrl * @param CustomerAccountManagement $customerAccountManagement * @param SubscriptionManagerInterface $subscriptionManager */ public function __construct( Context $context, SubscriberFactory $subscriberFactory, Session $customerSession, StoreManagerInterface $storeManager, CustomerUrl $customerUrl, CustomerAccountManagement $customerAccountManagement, SubscriptionManagerInterface $subscriptionManager ) { $this->subscriptionManager = $subscriptionManager; parent::__construct( $context, $subscriberFactory, $customerSession, $storeManager, $customerUrl, $customerAccountManagement, $subscriptionManager ); } /** * New subscription action * * @return Redirect */ public function execute() { if ($this->getRequest()->isPost() && $this->getRequest()->getPost('email')) { $email = (string) $this->getRequest()->getPost('email'); $name = (string) $this->getRequest()->getPost('name'); try { $this->validateEmailFormat($email); $this->validateGuestSubscription(); $this->validateEmailAvailable($email); $websiteId = (int) $this->_storeManager->getStore()->getWebsiteId(); /** @var Subscriber $subscriber */ $subscriber = $this->_subscriberFactory->create()->loadBySubscriberEmail($email, $websiteId); if ($subscriber->getId() && (int) $subscriber->getSubscriberStatus() === Subscriber::STATUS_SUBSCRIBED) { throw new LocalizedException( __('This email address is already subscribed.') ); } $storeId = (int) $this->_storeManager->getStore()->getId(); $currentCustomerId = $this->getSessionCustomerId($email); $subscriber = $currentCustomerId ? $this->subscriptionManager->subscribeCustomer($currentCustomerId, $storeId) : $this->subscriptionManager->subscribe($email, $storeId); if ($subscriber->getSubscriberId() > 0) { $subscriber = $this->_subscriberFactory->create()->loadBySubscriberEmail($email, $websiteId); $subscriber->setFullName($name)->save(); } $message = $this->getSuccessMessage((int) $subscriber->getSubscriberStatus()); $this->messageManager->addSuccessMessage($message); } catch (LocalizedException $e) { $this->messageManager->addComplexErrorMessage( 'localizedSubscriptionErrorMessage', ['message' => $e->getMessage()] ); } catch (\Exception $e) { $this->messageManager->addExceptionMessage($e, __('Something went wrong with the subscription.')); } } /** @var Redirect $redirect */ $redirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT); $redirectUrl = $this->_redirect->getRedirectUrl(); return $redirect->setUrl($redirectUrl); } /** * Get customer id from session if he is owner of the email * * @param string $email * @return int|null */ private function getSessionCustomerId(string $email): ? int { if (!$this->_customerSession->isLoggedIn()) { return null; } $customer = $this->_customerSession->getCustomerDataObject(); if ($customer->getEmail() !== $email) { return null; } return (int) $this->_customerSession->getId(); } /** * Get success message * * @param int $status * @return Phrase */ private function getSuccessMessage(int $status) : Phrase { if ($status === Subscriber::STATUS_NOT_ACTIVE) { return __('The confirmation request has been sent.'); } return __('Thank you for your subscription.'); } } |
Magemonkeys/Newsletter/view/adminhtml/layout/newsletter_subscriber_block.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?xml version="1.0"?> <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd"> <body> <referenceBlock name="adminhtml.newslettrer.subscriber.grid.columnSet"> <block class="Magento\Backend\Block\Widget\Grid\Column" name="adminhtml.newslettrer.subscriber.grid.columnSet.full.name" as="full.name"> <arguments> <argument name="header" xsi:type="string" translate="true">Name</argument> <argument name="index" xsi:type="string">full_name</argument> <argument name="header_css_class" xsi:type="string">col-name</argument> <argument name="column_css_class" xsi:type="string">ccol-name</argument> </arguments> </block> </referenceBlock> </body> </page> |
Magemonkeys/Newsletter/view/frontend/templates/subscribe.phtml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
<?php /** @var \Magento\Newsletter\Block\Subscribe $block */ ?> <div class="block newsletter"> <div class="title"><strong><?php /* @escapeNotVerified */ echo __('Newsletter') ?></strong></div> <div class="content"> <form class="form subscribe" novalidate action="<?php echo $block->escapeUrl($block->getFormActionUrl()) ?>" method="post" data-mage-init='{"validation": {"errorClass": "mage-error"}}' id="newsletter-validate-detail"> <div class="field full_name"> <label class="label" for="full_name"><span><?php echo $block->escapeHtml(__('Name')) ?></span></label> <div class="control"> <input name="full_name" type="text" id="full_name" placeholder="<?php echo $block->escapeHtmlAttr(__('Name')) ?>" data-validate="{required:true}"/> </div> </div> <div class="actions"> <button class="action subscribe primary" title="<?php echo $block->escapeHtmlAttr(__('Subscribe')) ?>" type="submit"> <span><?php echo $block->escapeHtml(__('Subscribe')) ?></span> </button> </div> </form> </div> </div> |
Result :
[crayon-641fb746309d5493177549/] Using above fucntion Images can be imported directly from...
Override view block using di.xml and add the below code...
You can check a list of called layout XML for...
Follow the below steps to install and set up PWA...
If you want to remove all leading zero's from order,...
Let our Magento expert connect to discuss your requirement.
We offer Magento
certified developers.
Our Magento clientele
is 500+.
We sign NDA for the
security of your projects.
We’ve performed 100+
Magento migration projects.
Free quotation
on your project.
Three months warranty on
code developed by us.