1

I need to invoice my order automatically whenever shipment is done, but if say I ship only one item of multiple items order then invoice should also be created for only that item not for all. I am using following code after setting a observer whenever shipped but it is invoicing all items even if I ship just one:

public function invoicedStatusChange($observer)
    {
       $shipment = $observer->getEvent()->getShipment();
       $order = $shipment->getOrder();
       if($order->canInvoice())
       {
           $invoice = Mage::getModel('sales/service_order', $order)->prepareInvoice();
           $invoice->setRequestedCaptureCase(Mage_Sales_Model_Order_Invoice::CAPTURE_ONLINE);
           $invoice->register();
           $transactionSave = Mage::getModel('core/resource_transaction')
           ->addObject($invoice)
           ->addObject($invoice->getOrder());
           $transactionSave->save();
       }

Thanks in advance

1

Take a look at https://magento.stackexchange.com/a/11075/519

You need to include the qty of each item shipped

if($order->canInvoice()){
  foreach(get item collection just ship){
    $qtys[$item->getId()] = $item->getQtyShipped();
  }

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

}

Take a look at database table sales_flat_order_item

qty_canceled
qty_invoiced
qty_ordered
qty_refunded
qty_shipped

Note: you may need to get the qty from the $shipment, since the above fields store the total

  • I am new to magento, so can you please elaborate, as now how can i fetch this value for $qty_to_invoice – Inder Jun 30 '15 at 14:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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