We sacrifice by not doing any other technology, so that you get the best of Magento.

We sacrifice by not doing any other technology, so that you get the best of Magento.

    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

    <?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="MagentoNewsletterModelSubscriber" type="MagemonkeyNewsletterModelSubscriber" />
    		
    </config>

    Step 2: Paste the below code in Your Subscriber.php under Magemonkey/Newsletter/Model/Subscriber.php

    <?php
    
    namespace MagemonkeyNewsletterModel;
    
    use MagentoFrameworkAppObjectManager;
    
    class Subscriber extends MagentoNewsletterModelSubscriber
    {
        /**
         * @var MagentoCustomerModelGroupFactory
         */
        protected $_customerGroup;
    
        /**
         * @var MagentoStoreModelWebsite
         */
        protected $_website;
    
        /**
         * @var MagentoSalesRuleModelRule
         */
        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,
                MagentoStoreModelScopeInterface::SCOPE_STORE
            ) || !$this->_scopeConfig->getValue(
                self::XML_PATH_SUCCESS_EMAIL_IDENTITY,
                MagentoStoreModelScopeInterface::SCOPE_STORE
            )
            ) {
                return $this;
            }
    
            $this->inlineTranslation->suspend();
    
            $this->_transportBuilder->setTemplateIdentifier(
                $this->_scopeConfig->getValue(
                    self::XML_PATH_SUCCESS_EMAIL_TEMPLATE,
                    MagentoStoreModelScopeInterface::SCOPE_STORE
                )
               
            )->setTemplateOptions(
                [
                    'area' => MagentoFrameworkAppArea::AREA_FRONTEND,
                    'store' => $this->_storeManager->getStore()->getId(),
                ]
            )->setTemplateVars(
                ['subscriber' => $this, 'coupon_code' => $this->generateCouponCode()]
            )->setFrom(
                $this->_scopeConfig->getValue(
                    self::XML_PATH_SUCCESS_EMAIL_IDENTITY,
                    MagentoStoreModelScopeInterface::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 MagentoSalesRuleModelRule $rule */
                $rule = $this->_getSalesRule();
                $couponCode = $rule->getCouponCodeGenerator()->setLength(4)->setAlphabet(
                    'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
                )->generateCode().'WELCOME';
                
                /*$rule->loadPost($couponData);
                $rule->save();*/
                $objectManager = MagentoFrameworkAppObjectManager::getInstance(); 
                $datetime = $objectManager->get('MagentoFrameworkStdlibDateTime');
                $date = $objectManager->get('MagentoFrameworkStdlibDateTimeDateTime');
            	$nowTimestamp = $datetime->formatDate($date->gmtTimestamp());
                $coupon = $objectManager->create('MagentoSalesRuleModelCoupon')
    				->setRuleId(81)
                    ->setUsageLimit(NULL)
                    ->setUsagePerCustomer(1)
                    ->setCreatedAt($nowTimestamp)
                    ->setType(MagentoSalesRuleHelperCoupon::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 MagentoCustomerModelGroup
         */
        protected function _getCustomerGroup()
        {
            if ($this->_customerGroup === null) {
                $this->_customerGroup = ObjectManager::getInstance()->get(MagentoCustomerModelGroup::class);
            }
            return $this->_customerGroup;
        }
    
        /**
         * @return MagentoStoreModelWebsite
         */
        protected function _getWebsite()
        {
            if ($this->_website === null) {
                $this->_website = ObjectManager::getInstance()->get(MagentoStoreModelWebsite::class);
            }
            return $this->_website;
        }
    
        /**
         * @return MagentoSalesRuleModelRule
         */
        protected function _getSalesRule()
        {
            if ($this->_salesRule === null) {
                $this->_salesRule = ObjectManager::getInstance()->get(MagentoSalesRuleModelRule::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.

    field_5bfb909c5ccae

      Get a Free Quote