在Magento 2.2.3中付款后自动发送发票电子邮件

时间:2019-02-20 20:17:52

标签: php event-handling observers magento2.2.3

我已尝试在支付订单并创建发票后触发客户发票邮件。支付网关是PayPal Plus(iways)和Amazon Pay。如果我理解正确,那么一旦成功处理了订单,这些网关就会自动创建发票。

这是我编写的模块,但是激活它并清除缓存后什么也没发生:

app / code / Vendor / AutoSendInvoice / etc / events.xml

    <?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
        <event name="sales_order_invoice_register">
            <observer name="VendorAutoSendInvoice" instance="Vendor\AutoSendInvoice\Observer\AutoSendInvoice" />
        </event>
    </config>

这是Observer应用程序/代码/供应商/AutoSendInvoice/Observer/AutoSendInvoice.php

<?php
namespace Vendor\AutoSendInvoice\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Sales\Model\Order;
use Magento\Sales\Model\Order\Email\Sender\InvoiceSender;
use \Magento\Framework\Exception\LocalizedException;
use \Psr\Log\LoggerInterface;

class AutoSendInvoice implements ObserverInterface
{
    /**
     * @var \Magento\Sales\Model\OrderFactory
     */
    protected $orderModel;

    /**
     * @var \Magento\Sales\Model\Order\Email\Sender\InvoiceSender
     */
    protected $invoiceSender;

    /**
     * Logger
     * @var LoggerInterface
     */
    protected $logger;


    /**
     * @param \Magento\Sales\Model\OrderFactory $orderModel
     * @param InvoiceSender $invoiceSender
     * @param LoggerInterface $logger
     */
    public function __construct(
        \Magento\Sales\Model\OrderFactory $orderModel,
        \Magento\Sales\Model\Order\Email\Sender\InvoiceSender $invoiceSender,
        LoggerInterface $logger
    )
    {
        $this->orderModel = $orderModel;
        $this->invoiceSender = $invoiceSender;
        $this->logger = $logger;
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        // get the corresponding order & invoice
        $order = $observer->getEvent()->getOrder();
        $invoice = $observer->getEvent()->getInvoice();


        if (!$order->getId()) {
            throw new LocalizedException(__('The order no longer exists.'));
        }

        // send invoice email only for PayPal Plus and Amazon Pay AND if order status is "new" or "processing"
        if ( $this->checkPaymentMethod($order ) && $this->checkStateOrder($order )
            ) {
                try {
                    // check if order is allowed to create invoice
                    $this->checkOrder($order);

                    // send invoice email
                    try {
                        $this->invoiceSender->send($invoice);
                    } catch (\Exception $e) {
                        $this->logger->error($e->getMessage());
                    }

                    // add order comment
                    $order->addStatusHistoryComment(
                        'Automatically Invoiced by Vendor',
                        true
                    )->save();


                } catch (\Exception $e) {
                    $this->logger->error($e->getMessage());
                }
            }
        }


    /**
     * @param $order
     * @throws LocalizedException
     */
    protected function checkOrder($order)
    {
        if (!$order->canInvoice()
        ) {
            throw new LocalizedException(
                __('The order does not allow an invoice to be created.')
            );
        }
    }

    protected function checkPaymentMethod($order) {
        // get the payment method for corresponding order
        $payment = $order->getPayment()->getMethodInstance()->getCode();

        if ( $payment == 'iways_paypalplus_payment' || $payment == 'amazon_payment' ) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * Check State Order
     *
     * @param $order
     * @return bool
     */
    protected function checkStateOrder($order)
    {
        if ($order->getState() == Order::STATE_NEW || $order->getState() == Order::STATE_PROCESSING) {
            return true;
        } else {
            return false;
        }
    }
}

我没有收到任何错误。我在这里想念什么? o_O

1 个答案:

答案 0 :(得分:0)

可能存在错误:

  1. 检查是否在管理员->商店->配置->销售电子邮件->发票电子邮件中启用了发票电子邮件。
  2. 检查所有克朗是否工作