Question

I need to observe the invoice creation event and get only the invoiced item quantities since I use partial invoicing sometimes.

I tried the events sales_order_invoice_save_after and sales_order_invoice_pay and found that sales_order_invoice_save_after is fired every time a change is made to invoice. So i used sales_order_invoice_pay and got the items into a log using this code

$invoice = $observer->getEvent()->getInvoice();
foreach ($invoice->getAllItems() as $item) {
      $name = $item->getName();
      $type = $item->getSku();
      $id = $item->getProductId();
      $qty = $item->getQty();
      Mage::log($name.' '.$type.' '.$id.' '.$qty.' '.$parentId , null, 'test.log', true); 
}

Most of my products are configurable products. In the log file I get two lines for the same order, configurable product details and simple product details. Also the $qty value for the configurable product shows the invoiced quantity(in case of partial invoice) and the simple product $qty shows the ordered quantity.

How do I get only the simple product details with the invoiced qunatity?

Was it helpful?

Solution

James As you want you works in on invoice creation time you can use getOrigData('entity_id')

On creation on invoice it always give the null value and you can use sales_order_invoice_save_after events

so you try use

$invoiceId = $observer->getEvent()->getInvoice()->getOrigData('entity_id');
if(is_null($invoiceId )){
// do your logic
}

an for getting item qty try this

   foreach ($invoice->getAllItems() as $item){
                if ($item->getOrderItem()->getParentItem()) {
                    continue;
                }
                /* Draw item */

        $name = $item->getName();
        $type = $item->getSku();
        $id = $item->getProductId();
        $qty = $item->getQty();

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