Magento: Getting a shipment collection containing only shipments that contain a comment that contains a certain piece of text

Magento: Getting a shipment collection containing only shipments that contain a comment that contains a certain piece of text

September 18, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento: Getting a shipment collection containing only shipments that contain a comment that contains a certain piece of text

Working with Magento Order Shipments and Comments

In Magento, querying for specific order shipments based on associated comments can indeed be accomplished using the native model methods. This involves working with the Mage_Sales_Model_Mysql4_Order_Shipment_Collection class to filter the shipments based on the comments they contain.

Understanding the Relationship Between Entities

The key entities involved in this query are the order shipment, the shipment comment, and the content of the comment. These are connected through the shipment_id field in the sales_shipment_comment table.

To retrieve a collection of shipments that have a comment with specific text, we need to join these tables on the appropriate fields and apply the necessary filters.

Building the Query

The starting point for your query should be the Mage_Sales_Model_Mysql4_Order_Shipment_Collection object. This can be retrieved using Magento’s resource model:

$shipments = Mage::getResourceModel('sales/order_shipment_collection');

Adding Filters to the Collection

To filter this collection based on shipment comments, we need to join it with the sales_shipment_comment table and apply a condition on the content field. This can be achieved using the joinAttribute method:

$shipments->getSelect()
    ->join(
        array('comment' => $shipments->getTable('sales/shipment_comment')),
        'main_table.id = comment.shipment_id',
        array('comment.content')
    )
    ->where('comment.content LIKE ?', "%Your Specific Text%");

In this example, we’re joining the sales_shipment_comment table using the shipment ID as a condition. We then use the LIKE operator to find comments that contain a specific piece of text.

Loading the Collection

Once you’ve added all the necessary filters, load the collection:

$shipments->load();

This will return an array of shipments that match your criteria.

Conclusion

While Magento’s object-oriented approach may seem more complex than writing raw SQL queries, it provides a consistent and secure way to interact with the database. Using the joinAttribute method allows you to build complex join conditions directly in your PHP code, eliminating the need for manual SQL statements.

Remember that efficient filtering is crucial when working with large datasets in Magento. Always ensure that your queries are well-indexed to maintain optimal performance during data retrieval.

For more insights on optimizing Magento’s database interactions and improving site performance generally, I recommend checking out our guide on Magento Performance Optimization Techniques.

Database Orders

Our network