0

I am trying to create invoice using customer token. But getting following error.

No such entity with orderId = 31

This error is generated after execution of following code.

$transactionSave = $this->_transaction->addObject(
                    $invoice
                )->addObject(
                    $order
                );
$transactionSave->save(); //This statement causes error

How to resolve this issue?

3

hi Please try below code for generate Invoice programatically

        $orderId = 31; //order id for which want to create invoice
        $order = $this->_orderRepository->get($orderId);
        if($order->canInvoice()) {
            $itemsArray = ['80'=>2]; //here 80 is order item id and 2 is it's quantity to be invoice
            $shippingAmount = '10.00';
            $subTotal = '110.00';
            $baseSubtotal = '110.00';
            $grandTotal = '110.00';
            $baseGrandTotal = '110.00';
            $invoice = $this->_invoiceService->prepareInvoice($order, $itemsArray);
            $invoice->setShippingAmount($shippingAmount);
            $invoice->setSubtotal($subTotal);
            $invoice->setBaseSubtotal($baseSubtotal);
            $invoice->setGrandTotal($grandTotal);
            $invoice->setBaseGrandTotal($baseGrandTotal);
            $invoice->register();
            $transactionSave = $this->_transaction->addObject(
                $invoice
            )->addObject(
                $invoice->getOrder()
            );
            $transactionSave->save();
            $this->invoiceSender->send($invoice);   
            //send notification code
            $order->addStatusHistoryComment(
                __('Notified customer about invoice #%1.', $invoice->getId())
            )
            ->setIsCustomerNotified(true)
            ->save();

Please refer below link for that :- https://webkul.com/blog/how-to-programmatically-create-invoice-in-magento2/

| improve this answer | |

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.