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 Answer 1

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

2
  • I am new to magento, so can you please elaborate, as now how can i fetch this value for $qty_to_invoice Commented Jun 30, 2015 at 14:03
  • Need to generate the invoice automatically once the shipment is created. [Magento 2.3.3 EE] Commented Jan 18, 2021 at 13:03

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.