3

I'm testing on Magento 2.2.3 and I've created an observer for the event sales_order_save_after which I'm using to automatically create an invoice.

Here is the current error that I'm receiving after placing an order:

Order saving error: Rolled back transaction has not been completed correctly.

And my MyCompany/MyModule/Observer/SalesOrderSaveAfter.php

<?php
namespace MyCompany\MyModule\Observer;

use Magento\Framework\Event\ObserverInterface;

class SalesOrderSaveAfter implements ObserverInterface
{
    protected $_invoiceService;
    protected $_transactionFactory;

    public function __construct(
      \Magento\Sales\Model\Service\InvoiceService $invoiceService,
      \Magento\Framework\DB\TransactionFactory $transactionFactory
    ) {
       $this->_invoiceService = $invoiceService;
       $this->_transactionFactory = $transactionFactory;
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $order = $observer->getEvent()->getOrder();

        try {
            if(!$order->canInvoice()) {
                return null;
            }
            if(!$order->getState() == 'new') {
                return null;
            }

            $invoice = $this->_invoiceService->prepareInvoice($order);
            $invoice->setRequestedCaptureCase(\Magento\Sales\Model\Order\Invoice::CAPTURE_ONLINE);
            $invoice->register();

            $transaction = $this->_transactionFactory->create()
              ->addObject($invoice)
              ->addObject($invoice->getOrder());

            $transaction->save();

        } catch (\Exception $e) {
            $order->addStatusHistoryComment('Exception message: '.$e->getMessage(), false);
            $order->save();
            return null;
        }
    }
}

If I remove the transaction portion of the code, eg:

$transaction = $this->_transactionFactory->create()
    ->addObject($invoice)
    ->addObject($invoice->getOrder());

    $transaction->save();

then the order will pass through with the products marked as invoiced, but no invoice is actually created or saved to the order.

Any ideas what I could be missing?

  • 3
    The biggest problem is that your order hadn't yet been committed to the database and so your transaction results in a foreign key constraint error. (i.e. the sales_invoice table has an order_id column but your order hasn't yet been committed) Does this need to fired after all transactions or just those on the frontend? If the latter, try using the checkout_onepage_controller_success_action – Phil Birnie Mar 13 '18 at 4:20
  • 2
    @Aj47, Yes Phil Birnie is right. You should use checkout_onepage_controller_success_action action to create invoice – Prince Patel Mar 13 '18 at 5:25
  • @PhilBirnie ah that makes sense then, I actually need this to fire from orders placed from the admin as well. I've also tried sales_order_place_after but ran into the same issue. Is there an event that would work for admin orders? – AJ47 Mar 13 '18 at 13:11
  • 2
    looks like checkout_submit_all_after did the trick and I'll just move events.xml to adminhtml folder so it only fires from admin. Is that the best event to use for this application? – AJ47 Mar 13 '18 at 14:19
  • I want to create an invoice and send mail when order shipped. Have you any idea about this? – Dhaduk Mitesh Feb 13 at 9:05
5

As pointed out, the answer to this is that I was using the wrong event. With the event sales_order_save_after the order hasn't been committed to the Database yet.

I changed my event to fire on checkout_submit_all_after and my observer is now working.

  • Do you understand why after the sales_order_save_after event the order hasn't been committed to the database? – LucScu Jun 6 '18 at 8:05
  • @LucaS Unfortunately, I'm not 100% sure why this is the case -- I know in Magento 1.8.x+ I could use sales_order_save_after event to fire off an automatic invoice. But not sure the difference in M2 – AJ47 Jun 6 '18 at 20:00
  • i tried this in magento2.3 but it not work – Jigs Parmar Feb 19 at 8:45
  • @JigsParmar , just tested on our 2.3 store and seems to still be working. You may have something else going on. Feel free to post a separate question and I can take a look! – AJ47 Feb 19 at 14:23
  • its working now , i trying to generate shipping programatically thats the issue – Jigs Parmar Feb 20 at 10:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.