cancel
Showing results for 
Search instead for 
Did you mean: 

Retrieve child product ID from invoice collection without using the product model

Retrieve child product ID from invoice collection without using the product model

Hi there

 

I am trying to create my own custom sales report which collates together a list of all items sold. I have the following code, which is a collection of invoices

 

	$collection = Mage::getModel('sales/order_invoice')->getCollection()
		 ->addAttributeToFilter('created_at', array('from'=>$collection_date_start, 'to'=>$collection_date_end));

	foreach($collection as $invoice){

		foreach ($invoice->getAllItems() as $item) {
			$item["product_id"];
			$item->getProductId();
		}

	}

 

You will notice a have a few variations of obtaining the product ID. My issue is that when the product is configurable, the configurable product ID is given (and not the child product ID). The Qty and SKU are the child product, but not the product ID (which I need to retrieve product attributes of the child product). 

 

How can I get the underlying child product ID from this code, without using "Mage::getModel('catalog/product')->load()" (which would very quickly exhaust memory limits since it would run on all items sold within a quarter)?

 

A few notes: 

  • when a use print_r on $item, I can in fact see the child product ID in the output but I just don't know how to access it; 
  • I wonder if using a collection of orders gives me access to more functions (and in which case, I need to determine whether those having been invoiced);
  • I heard that you can use a config.xml to have product attributes load by default in a collection, but I have not found a great deal of information on this, but this would be an alternative since I'm just trying to bring in product attributes of the child. 

Thanks!

M. 

1 REPLY 1

Re: Retrieve child product ID from invoice collection without using the product model

Hi @Marked13,

 

I guess you should check items into the Order and not only into the Invoice.

The order items table has that kind of information (you can ask for products with parent_item_id. Rows with values on that row are simple or virtual products and when the column is empty the item is a complex product type.