Magento – How to Access Shipment from Order ID and product ID

ordersproductsshipment

In my activity, I can have several shipments per order. Shipments are generated automattically by a module I coded.

I now would like to access the shipment linked to a selected order (ID) and containing a selected product (ID).

I thought using the "AddAttributeToFilter" function, but I do not know how to use them exactly in this context.

Thank you for your help,

Best Answer

You can get shipments using

$order = Mage::getModel('sales/order')->load($orderId);
$shipments = Mage::getResourceModel('sales/order_shipment_collection')
        ->setOrderFilter($order)
        ->load();

Or

$order = Mage::getModel('sales/order')->load($orderId);
$order->getShipmentsCollection();

Shipment products can be fetch by

foreach($shipments as $shipment){
    foreach ($shipment->getAllItems() as $product){
        echo $product->getName() . "<br/>";
    }
}