0

get coupon code in the newsletter email template.

enter image description here

| improve this question | | | | |
0

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.

app/code/VendorName/ModuleName/etc/frontend/events.xml

app/code/VendorName/ModuleName/Observer/ObserverforAddCustomVariable.php

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}}
| improve this answer | | | | |

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.