Magento 2 Module


How to include coupon code in newsletter subscription success email in Magento 2


Hide/Show description

Hi everybody, I am Cuong, Welcome back to my Magento 2 tutorial video series. #magento2tutorial, #cartrule In the previous lesson, I showed everybody, How to create customization cart price rule condition in Magento 2. If you don't watch this tutorial yet, you can watch it here http://bit.ly/2G9MbNS Today, I am going to show everybody the best practice, How to include coupon code in newsletter subscription success email in Magento 2. By default, When the customer has been successfully subscribed to the newsletter, there will be an email notification. If your project requires to add the coupon code to this email, then this practice will help you to complete that requirement. We have the scenario: We will generate a coupon code by programmatically and it will be added into the newsletter subscription success email. So what will we do in this practice? 1. We will create a new module called PHPCuong_Newsletter 2. We will override the method named sendConfirmationSuccessEmail by using the "preference" 3. We will override the email template named subscr_success.html by using a customization module. Okie, let's go. Let's do this practice, you need to follow steps by step: Step 1: Declaring the new module called PHPCuong_Newsletter - Create the namespace PHPCuong in the path app\code - Create the module named Newsletter in the path app\code\PHPCuong - Create the file named registration.php in the path app\code\PHPCuong\Newsletter - Create the file named module.xml in the path app\code\PHPCuong\Newsletter\etc End of step #1, I have been completed the codes to create the new module called PHPCuong_Newsletter. Step 2: Override the method named sendConfirmationSuccessEmail - Create the new file named di.xml in the path app\code\PHPCuong\Newsletter\etc\frontend - Create the new file named Subscriber.php in the path app\code\PHPCuong\Newsletter\Model Step 3: Override the email template named subscr_success.html - By default, Magento 2 defines the newsletter subscription success email in the module named Magento_Newsletter and it is located in the path vendor\magento\module-newsletter\etc\email_templates.xml and the path vendor\magento\module-newsletter\view\frontend\email\subscr_success.html - Let's override this email template, we need to create these files in our module. - Create the new file named email_templates.xml in the path app\code\PHPCuong\Newsletter\etc - Create the new file named subscr_success.html in the path app\code\PHPCuong\Newsletter\view\frontend\email Step 4: Test and see the results 1. Run the following command lines: php bin/magento setup:upgrade --keep-generated php bin/magento cache:flush 2. Test the results Go to the storefront and subscribe newsletter. Go to the Magento Admin Panel → Marketing → Promotions → Cart Price Rules Yeah, it works perfectly. Thank you for your watching. If you have any questions about this practice, please feel free to leave a comment below. Don't forget to like, comment, share my videos and subscribe to my channel for getting the latest videos. Please do not hesitate to contact me, if you need me to join your Magento project. My rate is $25/hour in Magento 1 and $30/hour in Magento 2. Get the source codes of this tutorial in the Github https://github.com/php-cuong/magento2-newsletter-coupon-code