Create Invoice programmatically in Magento2 using Object manager

<?php
    use Magento\Framework\App\Bootstrap;
    require 'app/bootstrap.php';
    $params = $_SERVER;
    $bootstrap = Bootstrap::create(BP, $params);
    $objectManager = $bootstrap->getObjectManager();
    $state = $objectManager->get('Magento\Framework\App\State');
    $state->setAreaCode('frontend');
    $now = new \DateTime();
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $InvoiceService = $objectManager->create('\Magento\Sales\Model\Service\InvoiceService');
    $transaction = $objectManager->create('\Magento\Framework\DB\Transaction');
    $loadorder = $objectManager->create('\Magento\Sales\Api\Data\OrderInterfaceFactory')->create();
    $order = $loadorder->loadByIncrementId('000001619');
    $invoice = $InvoiceService->prepareInvoice($order);
    $invoice->register();
    $invoice->save();
    $transactionSave = $transaction->addObject($invoice)->addObject($invoice->getOrder());
    $invoice->getOrder()->setIsInProcess(true);
    $transactionSave->save();
    $order->addStatusHistoryComment(__('Invoice has been created #%1.', $invoice->getId()))->setIsCustomerNotified(true)->save();
?>

Comments

  1. This extension generates the invoice automatically on order creation
    https://webpanda-solutions.com/auto-invoice-shipment.html

    ReplyDelete
  2. Thank you for sharing such useful information. I really enjoyed while reading your article and it is good to know the latest updates. Do post more. And also read about Magento 2 Development Company

    ReplyDelete
  3. Hats off to your presence of mind..I really enjoyed reading your blog. I really appreciate your information which you shared with us.
    Invoices Apps

    ReplyDelete

Post a Comment