Skip to content

Instantly share code, notes, and snippets.

@peterjaap
Last active January 23, 2017 05:41
Show Gist options
  • Save peterjaap/7578027 to your computer and use it in GitHub Desktop.
Save peterjaap/7578027 to your computer and use it in GitHub Desktop.
Split invoice in Magento into two invoices, one for virtual and one for non-virtual products
<?php
chdir(dirname(__FILE__));
require_once '../app/Mage.php';
Mage::app();
umask(0);
$resource = Mage::getModel('core/resource');
$db = $resource->getConnection('core_write');
$order = Mage::getModel('sales/order')->load(1);
$sendEmail = true;
$productModel = Mage::getModel('catalog/product');
$virtuals = $nonVirtuals = 0;
foreach($order->getAllItems() as $orderItem) {
if($orderItem->getQtyInvoiced()==$orderItem->getQtyOrdered()) continue; // skip already invoiced items
$item = Mage::getModel('sales/convert_order')->itemToInvoiceItem($orderItem);
$_product = $productModel->load($item->getProductId());
if($_product->getTypeId() == Mage_Downloadable_Model_Product_Type::TYPE_DOWNLOADABLE) {
// Virtual
$virtuals++;
$virtualQtys[$item->getOrderItemId()] = $orderItem->getQtyOrdered();
$nonVirtualQtys[$item->getOrderItemId()] = 0;
} else {
// Non-virtual
$nonVirtuals++;
$nonVirtualQtys[$item->getOrderItemId()] = $orderItem->getQtyOrdered();
$virtualQtys[$item->getOrderItemId()] = 0;
}
}
/* Save non-virtual invoice first; it will contain shipping costs */
try {
$nonVirtualInvoice = Mage::getModel('sales/service_order', $order)->prepareInvoice($nonVirtualQtys);
$nonVirtualInvoice->register(); // be sure to use register() instead of save(); otherwise the 0 qty items will be displayed as well and the individual order lines will not be set to 'invoiced'
$nonVirtualInvoice->setEmailSent($sendEmail);
$nonVirtualInvoice->getOrder()->setCustomerNoteNotify($sendEmail);
$transactionSave = Mage::getModel('core/resource_transaction')
->addObject($nonVirtualInvoice)
->addObject($nonVirtualInvoice->getOrder())
->save();
} catch(Exception $e) {
die($e->getMessage());
}
/* If it has been split, save the virtual invoice as well */
if($virtuals > 0 && $nonVirtuals > 0) {
try {
$virtualInvoice = Mage::getModel('sales/service_order', $order)->prepareInvoice($virtualQtys);
$virtualInvoice->register(); // be sure to use register() instead of save(); otherwise the 0 qty items will be displayed as well and the individual order lines will not be set to 'invoiced'
$virtualInvoice->setEmailSent($sendEmail);
$virtualInvoice->getOrder()->setCustomerNoteNotify($sendEmail);
$transactionSave = Mage::getModel('core/resource_transaction')
->addObject($virtualInvoice)
->addObject($virtualInvoice->getOrder())
->save();
} catch(Exception $e) {
die($e->getMessage());
}
}
if($virtuals > 0 && $nonVirtuals > 0) {
echo('Order ' . $order->getIncrementId() . ' has been split into ' . $nonVirtualInvoice->getIncrementId() . ' and ' . $virtualInvoice->getIncrementId());
} elseif($nonVirtuals>0 && $virtuals == 0) {
echo('Order ' . $order->getIncrementId() . ' has been transformed into one non-virtual invoice; ' . $invoice->getIncrementId());
} elseif($virtuals > 0 && $nonVirtuals == 0) {
echo('Order ' . $order->getIncrementId() . ' tried to be invoiced but has virtuals but no non-virtuals; this should be handled elsewhere');
} else {
echo('Order ' . $order->getIncrementId() . ' has no items to invoice');
}
echo "\n";
@Mydoacnt
Copy link

It works fine. I have rewrite invoiceController it is working for me but having issue of subtotal in separate invoice.Separate invoice display total of it's order.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment