문제

Is it possible to get the invoice from an order item?

To be more specific: I'm iterating the order ItemsCollection and if the order-item has been invoiced

$item->getStatusId() == Mage_Sales_Model_Order_Item::STATUS_INVOICED 

I'd like to get the corresponding invoice for it. Especially the invoice ID.

Has anyone a solution for this?

Thank you!

edit: I'm iterating an Order-Item-Collection. If an order-item has been already invoiced, then I would like to look up, in which invoice it has been invoiced. If the order item has not been invoiced - I leave it as is. It is just the opposite way of iterating an invoice item collection to look up the corresponding order. There I would just say

$invoiceItem->getOrder()->getId();

and I would be perfectly done. It may seems ridiculous to do it the opposite way (looking up an invoice for an order item instead of iterating the invoice collection of the specific order), but I'd like to avoid refactoring the whole logic.

edit:

Thanks to Lee Saferite I have found the solution for my task:

$orderItems = $order->getItemsCollection();
try {
    if (!$orderItems->hasFlag('invoice_join')){
        $orderItems->getSelect()->joinLeft(
            array('oii' => $orderItems->getTable('sales/invoice_item')),
            'main_table.item_id = oii.order_item_id',
            array('invoice_id' => 'parent_id')
        );
        $orderItems->getSelect()->group('main_table.item_id');
        $orderItems->clear();
        $orderItems->setFlag('invoice_join');
    }
}
catch(Exception $e){
    Mage::logException($e);
}

Due to the fact, that I'm calling this code inside a big foreach loop (I'm just extending an existing module - had to deal with it) I set a flag, to avoid the problem, that the join get's applied multiple times (which would end in an exception). Thanks Lee!

올바른 솔루션이 없습니다

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 magento.stackexchange
scroll top