Displaying Child Products in Magento’s “Products Ordered” Report
When working with configurable products in Magento, you might face challenges when generating sales reports like “Products Ordered”. By default, Magento aggregates the quantity sold for all child products into a single line item for the parent product in the report.
In this post, we’ll guide you through modifying Magento 1.6.2 to display both the parent configurable product and its associated child products in the report, along with their respective quantities sold.
Understanding the Current Behavior
Before diving into the solution, let’s clarify what happens under the hood:
- When a configurable product is ordered, only one record (product ID) is inserted into the
sales_flat_order_itemtable. - The quantity field contains the total number of units ordered for all child products combined.
Modifying the Report to Display Child Products
To achieve our goal, we need to make two key changes:
- Update the order item record to include child product information.
- Modify the report generation process to fetch this data.
Step 1: Updating Order Item Records
When a configurable product is ordered, you can update the order item record to store information about each child product and its respective quantity.
First, in your observer for sales_order_place_after event (e.g., located at app/code/core/Mage/Sales/etc/config.xml):
<event name="sales_order_place_after">
<observer name="save_child_product_info" type="Your_Module_Model_Observer"/>
</event>
Then, in your observer class (Your_Module/Model/Observer.php):
public function saveChildProductInfo($observer)
{
$order = $observer->getEvent()->getOrder();
foreach ($order->getAllItems() as $item) {
if ($item->isConfigurable()) {
foreach ($item->getChildrenItems() as $childItemId => $childQty) {
// Save child product information
$childProduct = Mage::getModel('catalog/product')->load($childItemId);
// Update order item with child product data
$childOrderItem = Mage::getModel('sales/order_item')
->setData('parent_id', $item->getId())
->setParentItemId($item->getId())
->setProductId($childItemId)
->save();
}
}
}
return $this;
}
Step 2: Generating the Report with Child Product Data
Now, let’s modify the report generation process to fetch and display child product information.
In your report view file (e.g., located at app/code/core/Mage/Adminhtml/Block/Sales/Order/Grid.php):
// Override _prepareCollection method
protected function _prepareCollection()
{
$collection = Mage::getModel('sales/order_item')->getCollection();
$this->setCollection($collection);
// Add new fields for child product data
$collection->addFieldToFilter('parent_id', array('neq' => null))
->join(array('child_product' => 'catalog/product'), 'main_table.product_id = child_product.entity_id', array(
'child_sku' => 'sku',
'child_name' => 'name',
'child_qty_ordered' => new Zend_Db_Expr('IFNULL(main_table.qty_ordered, 0)')
));
return parent::_prepareCollection();
}
// Override _prepareColumns method to display child product data
protected function _prepareColumns()
{
// Add a column for the child SKU and name
$this->addColumn('child_sku', array(
'header' => Mage::helper('sales')->__('Child SKU'),
'index' => 'child_sku',
));
$this->addColumn('child_name', array(
'header' => Mage::helper('sales')->__('Child Name'),
'index' => 'child_name',
));
// Add a column for the child quantity ordered
$this->addColumn('child_qty_ordered', array(
'header' => Mage::helper('sales')->__('Child Qty Ordered'),
'index' => 'child_qty_ordered',
'renderer' => 'Your_Module_Block_Sales_Item_Renderer_Qty'
));
// ... rest of the _prepareColumns method
}
Don’t forget to create a renderer for the child quantity ordered column (Your_Module/Block/Sales/Item/Renderer/Qty.php):
class Your_Module_Block_Sales_Item_Renderer_Qty extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
{
public function render(Varien_Object $row)
{
if ($row->getParentItemId()) {
return $row->getChildQtyOrdered();
}
return '';
}
}
Conclusion
In this post, we demonstrated how to modify Magento 1.6.2 to display both the parent configurable product and its associated child products in the “Products Ordered” report.
By implementing these changes, you can now gain a more granular view of your sales data, allowing for better inventory management and strategic decision-making based on individual child product performance.
Remember that modifying core Magento files is not recommended for future upgrades. Consider extending the functionality via extensions or custom modules to maintain the flexibility to upgrade without losing your enhancements.
For further guidance on Magento 1.x development best practices, be sure to explore our comprehensive tutorials at Laravel Company.