【问题标题】:How to send an e-mail after adding the product to the cart in magento 2?在magento 2中将产品添加到购物车后如何发送电子邮件?
【发布时间】:2021-10-12 00:14:48
【问题描述】:

不幸的是,到目前为止,我是一个在 magento 中创建模块的完整初学者。 将商品添加到购物车后,我需要发送电子邮件。

据我了解,我需要使用 checkout_cart_product_add_after 事件

我创建了一些文件,但我不明白如何在将商品添加到购物车后发送电子邮件

我的/模块/etc/events.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="checkout_cart_product_add_after">
    <observer name="email_after_adding_product" instance="My\Module\Observer\SendEmailForCart"/>
</event>

我的/模块/观察者/SendEmailForCart.php

<?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)
    {
        return $this->helperEmail->sendEmail();
    }
}

我的/模块/助手/Email.php

<?php

namespace My\Module\Helper;

class Email extends \Magento\Framework\App\Helper\AbstractHelper
{

    public function __construct(

    )
    {

    }

    public function sendEmail()
    {
        try {
            
        } catch (\Exception $e) {

        }
    }
}

请告诉我,我需要在 Email.php 文件中写什么代码? 我是否需要创建任何其他文件或修改上面显示的文件?

【问题讨论】:

    标签: php email magento events magento2


    【解决方案1】:
    /**
    * Recipient email config path
    */
    const XML_PATH_EMAIL_RECIPIENT = 'contact/email/recipient_email';
    /**
    * @var \Magento\Framework\Mail\Template\TransportBuilder
    */
    protected $_transportBuilder;
    
    /**
    * @var \Magento\Framework\Translate\Inline\StateInterface
    */
    protected $inlineTranslation;
    
    /**
    * @var \Magento\Framework\App\Config\ScopeConfigInterface
    */
    protected $scopeConfig;
    
    /**
    * @var \Magento\Store\Model\StoreManagerInterface
    */
    protected $storeManager;
    /**
    * @var \Magento\Framework\Escaper
    */
    protected $_escaper;
    
    /**
    * @param \Magento\Framework\App\Action\Context $context
    * @param \Magento\Framework\Mail\Template\TransportBuilder $transportBuilder
    * @param \Magento\Framework\Translate\Inline\StateInterface $inlineTranslation
    * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
    * @param \Magento\Store\Model\StoreManagerInterface $storeManager
    */
    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\Mail\Template\TransportBuilder $transportBuilder,
        \Magento\Framework\Translate\Inline\StateInterface $inlineTranslation,
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Framework\Escaper $escaper
    ) {
        parent::__construct($context);
        $this->_transportBuilder = $transportBuilder;
        $this->inlineTranslation = $inlineTranslation;
        $this->scopeConfig = $scopeConfig;
        $this->storeManager = $storeManager;
        $this->_escaper = $escaper;
    }
    
    /**
     * Post user question
     *
     * @return void
     * @throws \Exception
     */
    public function execute()
    {
        $post = $this->getRequest()->getPostValue();
        if (!$post) {
            $this->_redirect('*/*/');
            return;
        }
    
        $this->inlineTranslation->suspend();
    
        try {
            $postObject = new \Magento\Framework\DataObject();
            $postObject->setData($post);
            $error = false;
    
            $sender = [
                'name' => $this->_escaper->escapeHtml($post['name']),
                'email' => $this->_escaper->escapeHtml($post['email']),
            ];
    
            $storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORE; 
            $transport = $this->_transportBuilder
                ->setTemplateIdentifier('send_email_email_template') // this code we have mentioned in the email_templates.xml
                ->setTemplateOptions(
                    [
                        'area' => \Magento\Framework\App\Area::AREA_FRONTEND, // this is using frontend area to get the template file
                        'store' => \Magento\Store\Model\Store::DEFAULT_STORE_ID,
                    ]
                )
                ->setTemplateVars(['data' => $postObject])
                ->setFrom($sender)
                ->addTo($this->scopeConfig->getValue(self::XML_PATH_EMAIL_RECIPIENT, $storeScope))
                ->getTransport();
    
                $transport->sendMessage();
                $this->inlineTranslation->resume();
                $this->messageManager->addSuccess(
                    __('Thanks for contacting us with your comments and questions. We\'ll respond to you very soon.')
                );
                $this->_redirect('*/*/');
                return;
        } catch (\Exception $e) {
            $this->inlineTranslation->resume();
            $this->messageManager->addError(__('We can\'t process your request right now. Sorry, that\'s all we know.'.$e->getMessage())
            );
            $this->_redirect('*/*/');
            return;
        }
    }
    

    【讨论】:

    • 感谢您的回复!但是当我运行 setup:di:compile 时,编译器会报错:编译过程中的错误:My \ Module \ Helper \ Email 不兼容的参数类型:必需类型:\ Magento \ Framework \ App \ Helper \ Context。当前类型:\Magento\Framework\App\Action\Context;文件:/var/www/html/magento2/app/code/My/Module/Helper/Email.ph 我试图从\ Magento \ Framework \ App \ Action \ Context on \ Magento \ Framework \ App更改到Context的路径\Helper\Context 之后编译成功,但是商品没有加入购物车。
    【解决方案2】:

    在供应商/magento 中自行进行研究。有很多地方可以发送电子邮件。寻找 $transport 或 $transportObject 变量(不记得了)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-18
      • 2016-12-19
      • 2023-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-11
      • 1970-01-01
      相关资源
      最近更新 更多