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?

2 Answers 2

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/

0

There is a default Magento REST API available to create shipment/invoice for the order. You can use it as follows.

POST to URL 
https://vaseemansari.com/rest/V1/order/order_id/invoice
https://vaseemansari.com/rest/V1/order/1199305/invoice


Body 
{
    "capture": true,
    "items": [
        {
            "extension_attributes": {},
            "order_item_id": 2679144,   // This is the sales_order_item ID
            "qty": 1
        }
    ],
    "appendComment": true,
    "comment": {
        "extension_attributes": {},
        "comment": "Order Invoice generated via REST API",
        "is_visible_on_front": 0
    },
    "arguments": {
        "extension_attributes": {}
    },
    "notify": false
}

Or you can try this CURL as well

curl --location --request POST 'https://vaseemansari.com/rest/V1/order/1199305/invoice' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer bobkb32j4uv5ym3px6nfjkg5jzmoy7cc' \
--header 'Cookie: PHPSESSID=kafslfq30gv0gf7uptb92rjmnf; PHPSESSID=kafslfq30gv0gf7uptb92rjmnf; form_key=OOuH11iCJ3nLS7qS; persistent_shopping_cart=OWDN0gl7nnrU8lGZPHG2cFhzd6xS57WIwDY5zvJIwsMkvWeobu; private_content_version=9f66c6ed545f829348df1e1d26bcf368' \
--data-raw '{
    "capture": true,
    "items": [
        {
            "extension_attributes": {},
            "order_item_id": 2679144,
            "qty": 1
        }
    ],
    "appendComment": true,
    "comment": {
        "extension_attributes": {},
        "comment": "Order Invoice generated via REST API",
        "is_visible_on_front": 0
    },
    "arguments": {
        "extension_attributes": {}
    },
    "notify": false
}'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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