Question

I need to create all suborder invoice of an order in magento. i used this code but my amount is coming 0 .

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

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


   foreach ($order->getAllItems() as $item) {
    $items[$item->getId()] = $item->getQtyOrdered();
    }


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

// The rest is only required when handling a partial invoice as in this example
$amount = $invoice->getGrandTotal();
echo $amount;
$invoice->register()->pay();
$invoice->getOrder()->setIsInProcess(true);

$history = $invoice->getOrder()->addStatusHistoryComment(
    'Partial amount of $' . $amount . ' captured automatically.', false
);

$history->setIsCustomerNotified(true);

$order->save();

Mage::getModel('core/resource_transaction')
    ->addObject($invoice)
    ->addObject($invoice->getOrder())
    ->save();

How to create all suborder invoice of an order.... like if my order id 110101 and if contain 5 sku (101,102,103,104,105) for this i want to create suborder invoice of order in magento......

but when i apply the given answer my amount is coming 0. enter image description here

Was it helpful?

Solution

If you want to create sku respective sub invoice then try this.

   foreach ($order->getAllItems() as $item) {
        // create invoice for sku MatchSKU 
       if($item->getSku()=='MatchSKU'):
       // example MatchSKU =101 if  
         $items[$item->getId()] = $item->getQtyOrdered();
         endif;
    }

Instead of :

 foreach ($order->getAllItems() as $item) {
    $items[$item->getId()] = $item->getQtyOrdered();
    }

If you want to each item respective invoice then try this:

foreach ($order->getAllItems() as $item) :
    $itemsQty = array();
    $itemsQty[$item->getId()] = $item->getQtyOrdered();

          if (!$order->canInvoice()) {
           echo  Mage::helper('sales')->__('Cannot do invoice for order.');
        }

        $invoice = $order->prepareInvoice($itemsQty);

        $invoice->register();

       // if want to add comment when invoice creating 
            $invoice->addComment($comment='Test comment', $email=true);

        // send  new  invoice email
            $invoice->setEmailSent(true);

        $invoice->getOrder()->setIsInProcess(true);

        try {
            $transactionSave = Mage::getModel('core/resource_transaction')
                ->addObject($invoice)
                ->addObject($invoice->getOrder())
                ->save();

            $invoice->sendEmail($email, ($includeComment ? $comment : ''));
        } catch (Mage_Core_Exception $e) {
           // there  have an error
        }

         $invoice->getIncrementId();


endforeach;
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top