get coupon code in the newsletter email template.
1 Answer
You can create a custom module that adds a new variable to the email template.
- 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>
- 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;
}
}
- Now, you can use the
{{var subscriber.getData('coupon_code')}}
variable in your email template to display the coupon code.