在Magento 2.2支付订单后,自动发送发票电子邮件



我试图在付款并创建发票后触发客户发票邮件。付款网关是PayPal Plus(Iways)和亚马逊工资。如果我正确理解的话,这些网关在成功处理订单后会自动创建发票。

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

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="VendorAutoSendInvoiceObserverAutoSendInvoice" />
        </event>
    </config>

这是观察者应用/代码/供应商/autosendinvoice/observer/autosendinvoice.php

<?php
namespace VendorAutoSendInvoiceObserver;
use MagentoFrameworkEventObserverInterface;
use MagentoSalesModelOrder;
use MagentoSalesModelOrderEmailSenderInvoiceSender;
use MagentoFrameworkExceptionLocalizedException;
use PsrLogLoggerInterface;
class AutoSendInvoice implements ObserverInterface
{
    /**
     * @var MagentoSalesModelOrderFactory
     */
    protected $orderModel;
    /**
     * @var MagentoSalesModelOrderEmailSenderInvoiceSender
     */
    protected $invoiceSender;
    /**
     * Logger
     * @var LoggerInterface
     */
    protected $logger;

    /**
     * @param MagentoSalesModelOrderFactory $orderModel
     * @param InvoiceSender $invoiceSender
     * @param LoggerInterface $logger
     */
    public function __construct(
        MagentoSalesModelOrderFactory $orderModel,
        MagentoSalesModelOrderEmailSenderInvoiceSender $invoiceSender,
        LoggerInterface $logger
    )
    {
        $this->orderModel = $orderModel;
        $this->invoiceSender = $invoiceSender;
        $this->logger = $logger;
    }
    public function execute(MagentoFrameworkEventObserver $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;
        }
    }
}

我没有任何错误。我在这里缺少什么?

可能存在错误:

  1. 检查在管理中是否启用了发票电子邮件 - &gt;商店 - &gt;config-&gt;销售电子邮件 - &GT;发票电子邮件。
  2. 检查是否所有工作crons

相关内容

  • 没有找到相关文章

最新更新