Magento 2 Api通话

时间:2019-05-26 13:30:00

标签: php rest api web-services magento2

您好,我是magento 2的新手,非常感谢您的放心。我刚刚创建了一个模块,该模块负责以非常简单的形式替换magento的“联系我们”部分。我收到了电子邮件,但我希望它们由api rest管理。我不知道如何感谢您的回报。这是我发送的php代码。

const XML_PATH_EMAIL_RECIPIENT_NAME = 'trans_email/ident_support/name';
const XML_PATH_EMAIL_RECIPIENT_EMAIL = 'trans_email/ident_support/email';

protected $_inlineTranslation;
protected $_transportBuilder;
protected $_scopeConfig;
protected $_logLoggerInterface;


public function __construct(
    \Magento\Framework\App\Action\Context $context,
    \Magento\Framework\Translate\Inline\StateInterface $inlineTranslation,
    \Magento\Framework\Mail\Template\TransportBuilder $transportBuilder,
    \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
    \Psr\Log\LoggerInterface $loggerInterface,
    array $data = []

    )
{
    $this->_inlineTranslation = $inlineTranslation;
    $this->_transportBuilder = $transportBuilder;
    $this->_scopeConfig = $scopeConfig;
    $this->_logLoggerInterface = $loggerInterface;
    $this->messageManager = $context->getMessageManager();

    parent::__construct($context);     
}

public function execute()
{
    $post = $this->getRequest()->getPost();
    try
    {

        $this->_inlineTranslation->suspend();
        $storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORE;

        $sender = [
            'name' => $post['name'],
            'email' => $post['email'],
            ''
        ];

        $sentToEmail = $this->_scopeConfig ->getValue('trans_email/ident_general/email',\Magento\Store\Model\ScopeInterface::SCOPE_STORE);

        $sentToName = $this->_scopeConfig ->getValue('trans_email/ident_general/name',\Magento\Store\Model\ScopeInterface::SCOPE_STORE);


        $transport = $this->_transportBuilder
        ->setTemplateIdentifier('testemail_email_template')
        ->setTemplateOptions(
            [
                'area' => 'frontend',
                'store' => \Magento\Store\Model\Store::DEFAULT_STORE_ID,
            ]
            )
            ->setTemplateVars([
                'name'  => $post['name'],
                'email'  => $post['email']
            ])
            ->setFrom($sender)
            ->addTo($sentToEmail,$sentToName)
            ->addTo('abderrahmane.mammeri@technology-everywhere.fr','owner')
            ->getTransport();

            $transport->sendMessage();

            $this->_inlineTranslation->resume();
            $this->messageManager->addSuccess('Votre mail a bien été envoyé');
            $this->_redirect('testemail/index/index');

    } catch(\Exception $e){
        $this->messageManager->addError($e->getMessage());
        $this->_logLoggerInterface->debug($e->getMessage());
        exit;
    }

}

}

0 个答案:

没有答案