【问题标题】:How to attach invoice PDF instead of packing slip in magento 2如何在magento 2中附加发票PDF而不是装箱单
【发布时间】:2019-03-10 20:02:15
【问题描述】:

我想在创建货件时附上发票 pdf 而不是装箱单。 我正在使用 Fooman 电子邮件附件扩展版本 2.0.8

我的 magento 是 2.2.5 有人知道如何更改发货确认邮件中随附的 PDF 吗?

目前它正在附上装箱单,但我想在发货确认邮件中附上发票 pdf。

【问题讨论】:

    标签: magento2 email-attachments


    【解决方案1】:

    我们在使用 fooman 时遇到了类似的问题。我们还希望在创建发货时发送我们的发票,同时在创建发票时禁用标准交易发票邮件。我编写了一个模块,将发票电子邮件与发货电子邮件一起发送,这并不是您正在寻找的,但也许您可以利用它。

    该模块非常简单。除了样板的registration.php和module.xml,你只需要覆盖Magento\Sales\Model中的InvoiceOrder并注释掉这一行:

    $this->notifierInterface->notify($order, $invoice, $comment);

    像这样:

    <?php
    /**
     * Copyright © Magento, Inc. All rights reserved.
     * See COPYING.txt for license details.
     */
    
    namespace Magento\Sales\Model;
    
    use Magento\Framework\App\ResourceConnection;
    use Magento\Sales\Api\Data\InvoiceCommentCreationInterface;
    use Magento\Sales\Api\Data\InvoiceCreationArgumentsInterface;
    use Magento\Sales\Api\InvoiceOrderInterface;
    use Magento\Sales\Api\OrderRepositoryInterface;
    use Magento\Sales\Model\Order\Config as OrderConfig;
    use Magento\Sales\Model\Order\Invoice\NotifierInterface;
    use Magento\Sales\Model\Order\InvoiceDocumentFactory;
    use Magento\Sales\Model\Order\InvoiceRepository;
    use Magento\Sales\Model\Order\OrderStateResolverInterface;
    use Magento\Sales\Model\Order\PaymentAdapterInterface;
    use Magento\Sales\Model\Order\Validation\InvoiceOrderInterface as InvoiceOrderValidator;
    use Psr\Log\LoggerInterface;
    
    /**
     * Class InvoiceOrder
     * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
     */
    class InvoiceOrder implements InvoiceOrderInterface
    {
        /**
         * @var ResourceConnection
         */
        private $resourceConnection;
    
        /**
         * @var OrderRepositoryInterface
         */
        private $orderRepository;
    
        /**
         * @var InvoiceDocumentFactory
         */
        private $invoiceDocumentFactory;
    
        /**
         * @var PaymentAdapterInterface
         */
        private $paymentAdapter;
    
        /**
         * @var OrderStateResolverInterface
         */
        private $orderStateResolver;
    
        /**
         * @var OrderConfig
         */
        private $config;
    
        /**
         * @var InvoiceRepository
         */
        private $invoiceRepository;
    
        /**
         * @var InvoiceOrderValidator
         */
        private $invoiceOrderValidator;
    
        /**
         * @var NotifierInterface
         */
        private $notifierInterface;
    
        /**
         * @var LoggerInterface
         */
        private $logger;
    
        /**
         * InvoiceOrder constructor.
         * @param ResourceConnection $resourceConnection
         * @param OrderRepositoryInterface $orderRepository
         * @param InvoiceDocumentFactory $invoiceDocumentFactory
         * @param PaymentAdapterInterface $paymentAdapter
         * @param OrderStateResolverInterface $orderStateResolver
         * @param OrderConfig $config
         * @param InvoiceRepository $invoiceRepository
         * @param InvoiceOrderValidator $invoiceOrderValidator
         * @param NotifierInterface $notifierInterface
         * @param LoggerInterface $logger
         * @SuppressWarnings(PHPMD.ExcessiveParameterList)
         */
        public function __construct(
            ResourceConnection $resourceConnection,
            OrderRepositoryInterface $orderRepository,
            InvoiceDocumentFactory $invoiceDocumentFactory,
            PaymentAdapterInterface $paymentAdapter,
            OrderStateResolverInterface $orderStateResolver,
            OrderConfig $config,
            InvoiceRepository $invoiceRepository,
            InvoiceOrderValidator $invoiceOrderValidator,
            NotifierInterface $notifierInterface,
            LoggerInterface $logger
        ) {
            $this->resourceConnection = $resourceConnection;
            $this->orderRepository = $orderRepository;
            $this->invoiceDocumentFactory = $invoiceDocumentFactory;
            $this->paymentAdapter = $paymentAdapter;
            $this->orderStateResolver = $orderStateResolver;
            $this->config = $config;
            $this->invoiceRepository = $invoiceRepository;
            $this->invoiceOrderValidator = $invoiceOrderValidator;
            $this->notifierInterface = $notifierInterface;
            $this->logger = $logger;
        }
    
        /**
         * @param int $orderId
         * @param bool $capture
         * @param array $items
         * @param bool $notify
         * @param bool $appendComment
         * @param \Magento\Sales\Api\Data\InvoiceCommentCreationInterface|null $comment
         * @param \Magento\Sales\Api\Data\InvoiceCreationArgumentsInterface|null $arguments
         * @return int
         * @throws \Magento\Sales\Api\Exception\DocumentValidationExceptionInterface
         * @throws \Magento\Sales\Api\Exception\CouldNotInvoiceExceptionInterface
         * @throws \Magento\Framework\Exception\InputException
         * @throws \Magento\Framework\Exception\NoSuchEntityException
         * @throws \DomainException
         */
        public function execute(
            $orderId,
            $capture = false,
            array $items = [],
            $notify = false,
            $appendComment = false,
            InvoiceCommentCreationInterface $comment = null,
            InvoiceCreationArgumentsInterface $arguments = null
        ) {
            $connection = $this->resourceConnection->getConnection('sales');
            $order = $this->orderRepository->get($orderId);
            $invoice = $this->invoiceDocumentFactory->create(
                $order,
                $items,
                $comment,
                ($appendComment && $notify),
                $arguments
            );
            $errorMessages = $this->invoiceOrderValidator->validate(
                $order,
                $invoice,
                $capture,
                $items,
                $notify,
                $appendComment,
                $comment,
                $arguments
            );
            if ($errorMessages->hasMessages()) {
                throw new \Magento\Sales\Exception\DocumentValidationException(
                    __("Invoice Document Validation Error(s):\n" . implode("\n", $errorMessages->getMessages()))
                );
            }
            $connection->beginTransaction();
            try {
                $order = $this->paymentAdapter->pay($order, $invoice, $capture);
                $order->setState(
                    $this->orderStateResolver->getStateForOrder($order, [OrderStateResolverInterface::IN_PROGRESS])
                );
                $order->setStatus($this->config->getStateDefaultStatus($order->getState()));
                $invoice->setState(\Magento\Sales\Model\Order\Invoice::STATE_PAID);
                $this->invoiceRepository->save($invoice);
                $this->orderRepository->save($order);
                $connection->commit();
            } catch (\Exception $e) {
                $this->logger->critical($e);
                $connection->rollBack();
                throw new \Magento\Sales\Exception\CouldNotInvoiceException(
                    __('Could not save an invoice, see error log for details')
                );
            }
            if ($notify) {
                if (!$appendComment) {
                    $comment = null;
                }
                //$this->notifierInterface->notify($order, $invoice, $comment);
            }
            return $invoice->getEntityId();
        }
    }
    

    现在你要做的就是设置events.xml并观察sales_order_shipment_save_after

    <?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_shipment_save_after'>
            <observer name='SendInvoiceWithShipment' instance='Vendor\Module\Observer\SendInvoiceWithShipment'
            />
        </event>
    </config>
    

    Observer 像这样利用标准 Magento 2 交易电子邮件 $this-&gt;_invoiceSender-&gt;send($invoice);

    <?php
    
    namespace Vendor\Module\Observer;
    
    use Magento\Framework\Event\ObserverInterface;
    use Magento\Sales\Model\Order;
    use Magento\Sales\Model\Order\Email\Sender\InvoiceSender;
    
    class SendInvoiceWithShipment implements ObserverInterface
    {
        protected $_invoiceSender;
    
        public function __construct(
            InvoiceSender $invoiceSender
        ) {
            $this->_invoiceSender = $invoiceSender;
        }
    
        public function execute(\Magento\Framework\Event\Observer $observer)
        {
            $order = $observer->getShipment()->getOrder();
            if (!$order) {
                // Dont send invoice if order is not provided 
                return; 
            }
    
            $invoices = $order->getInvoiceCollection();
    
            foreach ($invoices as $invoice) {
    
                    try {
                        $this->_invoiceSender->send($invoice);
                    } catch (\Exception $e) {
                        // Do something if failed to send                          
                    }
    
            }               
    
            
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多