1

get coupon code in the newsletter email template.

enter image description here

1 Answer 1

0

You can create a custom module that adds a new variable to the email template.

  1. In your module, create an etc/di.xml file:
<?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="Magento\Newsletter\Model\Subscriber">
        <plugin name="addCouponCodeToNewsletter" type="Vendor\Newsletter\Plugin\Subscriber" sortOrder="10"/>
    </type>
</config>
  1. Create a Plugin/Subscriber.php file:
<?php
namespace Vendor\Newsletter\Plugin;

class Subscriber
{
    protected $ruleFactory;

    public function __construct(
        \Magento\SalesRule\Model\RuleFactory $ruleFactory
    ) {
        $this->ruleFactory = $ruleFactory;
    }

    public function aroundGetEmailConfirmationLink($subject, $proceed)
    {
        $couponCode = $this->getCouponCode();
        $subject->setData('coupon_code', $couponCode);
        return $proceed();
    }

    protected function getCouponCode()
    {
        // Replace with your logic to get the coupon code
        $couponCode = 'FIRSTFREE';
        return $couponCode;
    }
}
  1. Now, you can use the {{var subscriber.getData('coupon_code')}} variable in your email template to display the coupon code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.