Question

How I can get collection of items associated to that particular tracking numbers for each shipment of order using order id?

Was it helpful?

Solution

You need to keep in mind that each order can have multiple shipments and that each shipment can have multiple tracking numbers with multiple products.

Solution 1

After looking at app/code/core/Mage/Sales/Model/Order/Shipment.php you see that there are two methods of interest:

  • getAllItems() and
  • getAllTracks()

Which you can then use on your $order object

$order = Mage::getModel('sales/order')->load($your_order_id);
$shipment_collection = Mage::getResourceModel('sales/order_shipment_collection')
            ->setOrderFilter($order)
            ->load();
foreach($shipment_collection as $shipment){
    echo "Tracking number(s) for shipment:<br/>";
    foreach($shipment->getAllTracks() as $tracking_number){
        echo $tracking_number->getNumber() . "<br/>";
    }
    echo "Product(s) on shipment:<br/>";
    foreach ($shipment->getAllItems() as $product){
        echo $product->getName() . "<br/>";
    }
}

Solution 2 - Direct SQL Statements

The reason I'm including this is because I always find it very useful to check the database structure when I'm struggling with something. After a quick glance at the database, you see some notable tables namely:

  • sales_flat_shipment,
  • sales_flat_shipment_item and
  • sales_flat_shipment_track

Immediately with these tables in mind, you know pretty much exactly how to find what you are looking for by simply looking at their column names.

$order = Mage::getModel('sales/order')->load($your_order_id);
$sales_flat_shipment = $this->_getTableName('sales_flat_shipment');
$sales_flat_shipment_track = $this->_getTableName('sales_flat_shipment_track');
$sales_flat_shipment_item = $this->_getTableName('sales_flat_shipment_item');
$connection = $this->_getConnection('core_read');
$sql = 'SELECT entity_id FROM ' . $sales_flat_shipment . ' WHERE order_id = ?';
$shipments = $connection->fetchAll($sql, $your_order_id);
foreach($shipments as $shipment){
    $sql = 'SELECT * FROM ' . $sales_flat_shipment_track . ' WHERE parent_id = ?';
    $tracking = $connection->fetchAll($sql, $shipment['entity_id']);
    echo "Tracking number(s) for the order:<br/>";
    foreach ($tracking as $track){
        echo $track['track_number'] . "<br/>";
    }
    $sql = 'SELECT * FROM ' . $sales_flat_shipment_item . ' WHERE parent_id = ?';
    $items = $connection->fetchAll($sql, $shipment['entity_id']);
    echo "Product(s) on shipment:<br/>";
    foreach ($items as $item){
        echo $item['name'] . "<br/>";
    }
}

public function _getConnection($type = 'core_read'){
    return Mage::getSingleton('core/resource')->getConnection($type);
}

public function _getTableName($tableName){
    return Mage::getSingleton('core/resource')->getTableName($tableName);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top