Here you have to override the Subscriber Model so for that, you have to create a new module and override the Subscriber model?
If the answer is yes, then please do the below steps.
Step 1: Paste the below code in your di.xml
1 2 3 4 5 6 |
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <preference for="Magento\Newsletter\Model\Subscriber" type="Magemonkey\Newsletter\Model\Subscriber" /> </config> |
Step 2: Paste the below code in Your Subscriber.php under Magemonkey/Newsletter/Model/Subscriber.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 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
<?php namespace Magemonkey\Newsletter\Model; use \Magento\Framework\App\ObjectManager; class Subscriber extends \Magento\Newsletter\Model\Subscriber { /** * @var \Magento\Customer\Model\GroupFactory */ protected $_customerGroup; /** * @var \Magento\Store\Model\Website */ protected $_website; /** * @var \Magento\SalesRule\Model\Rule */ protected $_salesRule; /** * Sends out confirmation success email * * @return $this */ public function sendConfirmationSuccessEmail() { if ($this->getImportMode()) { return $this; } if (!$this->_scopeConfig->getValue( self::XML_PATH_SUCCESS_EMAIL_TEMPLATE, \Magento\Store\Model\ScopeInterface::SCOPE_STORE ) || !$this->_scopeConfig->getValue( self::XML_PATH_SUCCESS_EMAIL_IDENTITY, \Magento\Store\Model\ScopeInterface::SCOPE_STORE ) ) { return $this; } $this->inlineTranslation->suspend(); $this->_transportBuilder->setTemplateIdentifier( $this->_scopeConfig->getValue( self::XML_PATH_SUCCESS_EMAIL_TEMPLATE, \Magento\Store\Model\ScopeInterface::SCOPE_STORE ) )->setTemplateOptions( [ 'area' => \Magento\Framework\App\Area::AREA_FRONTEND, 'store' => $this->_storeManager->getStore()->getId(), ] )->setTemplateVars( ['subscriber' => $this, 'coupon_code' => $this->generateCouponCode()] )->setFrom( $this->_scopeConfig->getValue( self::XML_PATH_SUCCESS_EMAIL_IDENTITY, \Magento\Store\Model\ScopeInterface::SCOPE_STORE ) )->addTo( $this->getEmail(), $this->getName() ); $transport = $this->_transportBuilder->getTransport(); $transport->sendMessage(); $this->inlineTranslation->resume(); return $this; } /** * Retrieve the coupon code * * @return string */ protected function generateCouponCode() { try { /** @var \Magento\SalesRule\Model\Rule $rule */ $rule = $this->_getSalesRule(); $couponCode = $rule->getCouponCodeGenerator()->setLength(4)->setAlphabet( 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789' )->generateCode().'WELCOME'; /*$rule->loadPost($couponData); $rule->save();*/ $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $datetime = $objectManager->get('\Magento\Framework\Stdlib\DateTime'); $date = $objectManager->get('\Magento\Framework\Stdlib\DateTime\DateTime'); $nowTimestamp = $datetime->formatDate($date->gmtTimestamp()); $coupon = $objectManager->create('\Magento\SalesRule\Model\Coupon') ->setRuleId(81) ->setUsageLimit(NULL) ->setUsagePerCustomer(1) ->setCreatedAt($nowTimestamp) ->setType(\Magento\SalesRule\Helper\Coupon::COUPON_TYPE_SPECIFIC_AUTOGENERATED) ->setCode($couponCode) ->save(); return $couponCode; } catch (\Exception $e) { return null; } } /** * Retrieve the customer group ids * * @return array */ protected function getCustomerGroupIds() { $groupsIds = []; $collection = $this->_getCustomerGroup()->getCollection(); foreach ($collection as $group) { $groupsIds[] = $group->getId(); } return $groupsIds; } /** * Retrieve the website ids * * @return array */ protected function getWebsiteIds() { $websiteIds = []; $collection = $this->_getWebsite()->getCollection(); foreach ($collection as $website) { $websiteIds[] = $website->getId(); } return $websiteIds; } /** * @return \Magento\Customer\Model\Group */ protected function _getCustomerGroup() { if ($this->_customerGroup === null) { $this->_customerGroup = ObjectManager::getInstance()->get(\Magento\Customer\Model\Group::class); } return $this->_customerGroup; } /** * @return \Magento\Store\Model\Website */ protected function _getWebsite() { if ($this->_website === null) { $this->_website = ObjectManager::getInstance()->get(\Magento\Store\Model\Website::class); } return $this->_website; } /** * @return \Magento\SalesRule\Model\Rule */ protected function _getSalesRule() { if ($this->_salesRule === null) { $this->_salesRule = ObjectManager::getInstance()->get(\Magento\SalesRule\Model\Rule::class); } return $this->_salesRule; } } |
Here, we have passed the ‘coupon_code’ variable in the template vars.
This variable contains the unique coupon code for every subscriber. Also, we have used the object Manager but you can use the construct method. And we have passed the static RULE id in the code. You can also change the rule id.
[crayon-63d3e8164e28c991901395/] 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.