Magento 2如何实现商品加入购物车后自动发送邮件?
Magento加购发邮件功能实现方案
已有文件修改
1. 调整Observer文件(My/Module/Observer/SendEmailForCart.php)
当前Observer没有传递加购相关数据到邮件方法,修改后代码如下:
<?php namespace My\Module\Observer; use Magento\Framework\Event\ObserverInterface; use My\Module\Helper\Email; class SendEmailForCart implements ObserverInterface { private $helperEmail; public function __construct( Email $helperEmail ) { $this->helperEmail = $helperEmail; } public function execute(\Magento\Framework\Event\Observer $observer) { // 获取加购商品、购物车项数据 $product = $observer->getProduct(); $quoteItem = $observer->getQuoteItem(); // 传递数据到邮件发送方法 $this->helperEmail->sendEmail($product, $quoteItem); return $this; } }
需新增的文件
1. 邮件模板注册配置(My/Module/etc/email_templates.xml)
用于在Magento系统注册自定义邮件模板:
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Email:etc/email_templates.xsd"> <template id="add_to_cart_notification_email" label="加购通知邮件" file="add_to_cart_notification.html" type="html" module="My_Module" area="frontend"/> </config>
2. 邮件内容模板(My/Module/view/frontend/email/add_to_cart_notification.html)
自定义邮件的展示内容,可根据需求调整样式和变量:
<!--@subject 您已成功添加商品到购物车 @--> <!--@vars { "var product_name":"商品名称", "var product_sku":"商品SKU", "var qty":"加购数量", "var customer_email":"用户邮箱" } @--> <div> <h1>加购成功通知</h1> <p>您好,您已成功将以下商品添加到购物车:</p> <ul> <li>商品名称:{{var product_name}}</li> <li>商品SKU:{{var product_sku}}</li> <li>加购数量:{{var qty}}</li> </ul> <p>您可以前往购物车完成结算。</p> </div>
3. 完善Helper/Email.php代码
注入Magento核心邮件发送类,实现发送逻辑:
<?php namespace My\Module\Helper; use Magento\Framework\App\Helper\AbstractHelper; use Magento\Framework\App\Helper\Context; use Magento\Framework\Mail\Template\TransportBuilder; use Magento\Store\Model\StoreManagerInterface; use Magento\Framework\App\Config\ScopeConfigInterface; use Magento\Customer\Model\Session as CustomerSession; class Email extends AbstractHelper { const XML_PATH_EMAIL_SENDER = 'trans_email/ident_general/email'; const XML_PATH_EMAIL_SENDER_NAME = 'trans_email/ident_general/name'; protected $transportBuilder; protected $storeManager; protected $scopeConfig; protected $customerSession; public function __construct( Context $context, TransportBuilder $transportBuilder, StoreManagerInterface $storeManager, ScopeConfigInterface $scopeConfig, CustomerSession $customerSession ) { $this->transportBuilder = $transportBuilder; $this->storeManager = $storeManager; $this->scopeConfig = $scopeConfig; $this->customerSession = $customerSession; parent::__construct($context); } public function sendEmail($product, $quoteItem) { try { // 未登录用户不发送可自行删除该判断 if (!$this->customerSession->isLoggedIn()) { return; } $storeId = $this->storeManager->getStore()->getId(); // 获取发件人信息 $senderEmail = $this->scopeConfig->getValue(self::XML_PATH_EMAIL_SENDER, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $storeId); $senderName = $this->scopeConfig->getValue(self::XML_PATH_EMAIL_SENDER_NAME, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $storeId); // 获取收件人邮箱(当前登录用户邮箱) $recipientEmail = $this->customerSession->getCustomer()->getEmail(); // 组装邮件模板变量 $templateVars = [ 'product_name' => $product->getName(), 'product_sku' => $product->getSku(), 'qty' => $quoteItem->getQty(), 'customer_email' => $recipientEmail ]; // 构建邮件传输对象 $transport = $this->transportBuilder ->setTemplateIdentifier('add_to_cart_notification_email') ->setTemplateOptions(['area' => \Magento\Framework\App\Area::AREA_FRONTEND, 'store' => $storeId]) ->setTemplateVars($templateVars) ->setFrom(['email' => $senderEmail, 'name' => $senderName]) ->addTo($recipientEmail) ->getTransport(); // 发送邮件 $transport->sendMessage(); } catch (\Exception $e) { $this->_logger->error('加购邮件发送失败:' . $e->getMessage()); } } }
其他注意事项
- 若你还未创建模块注册文件,需要新增
My/Module/etc/module.xml完成模块注册,之后执行bin/magento setup:upgrade启用模块 - 功能开发完成后需执行
bin/magento cache:clean清空缓存后测试 - 可根据需求调整邮件模板的内容和变量,也可以修改收件人为其他指定邮箱
内容的提问来源于stack exchange,提问作者Vova
相关产品推荐
相关产品推荐

