How can I create an XML when an invoice has been created. Whenever order invoice gets created, it creates in a particular folder automatically.

1 Answer 1


Few things that you need to do is listed below:

  1. You need to create a simple module that will look for event sales_order_invoice_save_after. This event is triggered when invoice is generated. You can check tutorials to understand event observer concept from here or you can quickly create your module using module creator from here
  2. After creating the module Go to the file named Observer.php inside Model folder of your module.
  3. Now in that function of your Observer.php file use something like below to get the invoice id.

        $invoiceId = $observer->getEvent()->getInvoice()->getOrigData('entity_id');
        if(is_null($invoiceId )){
        // do your logic

    And you can get your item info

      foreach ($invoice->getAllItems() as $item){
                    if ($item->getOrderItem()->getParentItem()) {
            $name = $item->getName();
            $type = $item->getSku();
            $id = $item->getProductId();
            $qty = $item->getQty();

Now you get all the invoice information and you can generate your xml from here.

I think above answer helps you. If it does please do accept and vote up the answer.

Happy Coding!!

  • Its my pleasure!! Nov 15, 2017 at 12:01

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