Magento – Programmatically Invoice a Single Product from an Order

adminhtmlinvoiceorderssales

How could I go about programmatically invoicing a single product off of a sales order of more than one product?

I.E Order X has three products,
SKU1, SKU2 & SKU3. I only want to create an invoice for SKU1.

I've looked into

\app\code\core\Mage\Adminhtml\controllers\Sales\Order\InvoiceController.php

Though I get a little stuck when I see the _getItemQtys() method.

Could really use some advice.

Best Answer

$order = Mage::getModel('sales/order')->loadByIncrementId($increment_id);
$items = $order->getAllItems();

$qtys = array(); //this will be used for processing the invoice

foreach($items as $item){
    if ($item->getSku() == 'SKU1') {
        $qty_to_invoice = $item->getQtyOrdered(); // now gets order quantity of item
    } else {
        $qty_to_invoice = 0;
    }

    $qtys[$item->getId()] = $qty_to_invoice;
    <!-- Note that the ->getId() method gets the item_id on the order, not the product_id -->
}

$invoice = Mage::getModel('sales/service_order', $order)->prepareInvoice($qtys);

Enhanced the answer so it's more clear when you want only a specific SKU

Related Topic