Magento 2, Magento Development, Customization, Extension Development and Integration, Optimization, SEO and Responsive Design

Magento 2, Magento Development, Customization, Extension Development and Integration, Optimization, SEO and Responsive Design

How to create sales order invoice programmatically in magento


By default mangeto generate sales order invoice at the time when order payment is goes to paid.
But many times in magento some payment methods do not change order status to paid. So in this situation if you want to generate your order Invoice programmatically you can use following simple solution.

/*Create an invoice if not created yet*/
 $paymentMethod = $order->getPayment()->getMethodInstance()->getCode();

/*If you want to create only for few payment methods*/
if($paymentMethod=='YOUR PAYMENT METHOD''){
          if($order->hasInvoices()==0)
         {
               $invoice = Mage::getModel('sales/service_order', $order)->prepareInvoice();
               if (!$invoice->getTotalQty())
              {                          
                      $helper->log('Cannot create an invoice without products. '.$order->getRealOrderId());
                     $invoice-                 >setRequestedCaptureCase(Mage_Sales_Model_Order_Invoice::CAPTURE_OFFLINE);
                     $invoice->register();
                     $transactionSave = Mage::getModel('core/resource_transaction')
                     ->addObject($invoice)
                     ->addObject($invoice->getOrder());
                     $transactionSave->save();
              }
         }
}

That's it.

Please support us, Like us on Facebook.

0 comments:

Post a Comment

 

Copyright @ 2017 HKBlog.