get coupon code in the newsletter email template.

enter image description here


Yes,it is possible.

  1. Create A cart rules from admin > Marketing > Cart Rules.

2.Create observer event email_newsletter_set_template_vars_before.So, at this event you can add new parameter via transport to template means you can create coupon and send to email.



class ObserverforAddCustomVariable implements ObserverInterface {

protected $ruleFactory;
protected $massgenerator;
protected $logger;

public function __construct(\Magento\SalesRule\Model\RuleFactory $ruleFactory,\Magento\SalesRule\Model\Coupon\Massgenerator $massgenerator, \Psr\Log\LoggerInterface $logger ) {
    $this->ruleFactory= $ruleFactory;
    $this->massgenerator = $massgenerator;
    $this->logger = $logger;

 * @param \Magento\Framework\Event\Observer $observer
 * @return void
public function execute(\Magento\Framework\Event\Observer $observer)
    /** @var \Magento\Framework\App\Action\Action $controller */
    $transport = $observer->getTransport();
    $transport['free-coupon'] = "FREE2020";


  1. At the email template , you can get this custom variables free-coupon using {{var free-coupon|raw}}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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