/** * Return the qty of newly paid invoice items for gift card. * This method depends on giftcard_paid_invoice_items field in product options array. * It also update the field with the newly paid invoice items * * @param Mage_Sales_Model_Order_Item $item giftcard order item * @return int qty of newly paid invoice items */ protected function _getAndUpdatePaidInvoiceItems(Mage_Sales_Model_Order_Item $item) { $newlyPaidInvoiceItemQty = 0; $options = $item->getProductOptions(); $paidInvoiceItems = isset($options['giftcard_paid_invoice_items']) ? $options['giftcard_paid_invoice_items'] : array(); // find invoice for this order item $invoiceItemCollection = Mage::getResourceModel('sales/order_invoice_item_collection')->addFieldToFilter('order_item_id', $item->getId()); foreach ($invoiceItemCollection as $invoiceItem) { $invoiceId = $invoiceItem->getParentId(); if (isset($this->_loadedInvoices[$invoiceId])) { $invoice = $this->_loadedInvoices[$invoiceId]; } else { $invoice = Mage::getModel('sales/order_invoice')->load($invoiceId); $this->_loadedInvoices[$invoiceId] = $invoice; } // check, if this order item has been paid if ($invoice->getState() == Mage_Sales_Model_Order_Invoice::STATE_PAID && !in_array($invoiceItem->getId(), $paidInvoiceItems)) { $newlyPaidInvoiceItemQty += $invoiceItem->getQty(); $paidInvoiceItems[] = $invoiceItem->getId(); } } $options['giftcard_paid_invoice_items'] = $paidInvoiceItems; $item->setProductOptions($options); return $newlyPaidInvoiceItemQty; }