0

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

$order->getShipmentsCollection();

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()
                    ->getCollection()->addFieldToFilter("order_id",$orderId)->setOrder('entity_id','DESC');

1 Answer 1

1

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 and acknowledge you have read our privacy policy.

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