Normally you can get order shipment colletion like this from order:


but i'd like to retrieve the collection of shipments using order id, and i want to sort it by entity_id, so it will become like this:

$shipmentCollections = $this->shipmentModel->create()

To get shipments by order ID you can use \Magento\Sales\Model\ResourceModel\Order\Shipment\CollectionFactory:

protected $_shipmentCollection;

public function __construct(
    \Magento\Sales\Model\ResourceModel\Order\Shipment\CollectionFactory $shipmentCollection,
) {
    $this->_shipmentCollection = $shipmentCollection;

public function getShipmentsByOrderId($order_id) {
    $shipments = $this->_shipmentCollection->create()->addFieldToFilter('order_id', $order_id)->setOrder('entity_id', 'DESC');

    // Use $shipment->getItems() to iterate over results

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.