Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a order has a shipment in Magento?

Tags:

magento

I need to check if an order has already some shipment set. The only data I can use is the increment id of the order. I'm getting an instance of a model order, but I don't see a way I can get a shipment instance.

I'm using this code:

$order = Mage::getModel('sales/order')
    ->loadByIncrementId($order_increment_id);

But how can I get a shipment instance? I know that I can call Mage::getModel('sales/order_shipment')->loadByIncrementId($shipment_increment_id) but how do I get the shipment increment id?

like image 500
Italo André Avatar asked Sep 21 '11 20:09

Italo André


People also ask

How can I get Shipment details in Magento 2?

You can get the Shipment collection data for the specific shipment by shipment id in Magento 2. Using Magento\Sales\Api\ShipmentRepositoryInterface interface, you need to use get() method to fetch shipment data by its id. Pass shipment id as get method, get($shipmentId) arguments.

How to check invoice is generated or not for Magento2?

Go to Admin -> Sales -> Orders. Filter out all orders with the Processing status. Select the one you want to invoice: click View in the Action column. There is the Invoice option in the header of the sales order.

How to get order Tracking number in Magento 2?

After the order is placed, the admin can add order details and tracking number from the backend. Admin can also track order from the backend using the Order Tracking Extension for Magento 2. The customers can track order from their My Account section in a tabular detailed format.


2 Answers

Just to make it complete Mage_Sales_Model_Order has public method:
hasShipments()
which returns number of shipments and internally uses mentioned getShipmentsCollection().

like image 140
muffir Avatar answered Nov 11 '22 21:11

muffir


Assume that the person who wrote this might have also needed to do what you need to do. Generally, when Magento objects have a one to many relationship you can find a method to load the many on the one.

You've got a class alias sales/order.

This corresponds to Mage_Sales_Model_Order (in a stock installation).

You can find this class at app/code/core/Mage/Sales/Model/Order.php.

If you examine this class, there are 7 methods with the word "ship" in them

function canShip
function setShippingAddress
function getShippingAddress
function getShip
function getShipmentsCollection
function hasShip
function prepareShip

Of those 7, only the semantics of getShipmentsCollection indicate a method for grabbing an order's shipments. So try

foreach($order->getShipmentsCollection() as $shipment)
{
    var_dump(get_class($shipment));
    //var_dump($shipment->getData());
}

Or take a look at the source for getShipmentsCollection

public function getShipmentsCollection()
{
    if (empty($this->_shipments)) {
        if ($this->getId()) {
            $this->_shipments = Mage::getResourceModel('sales/order_shipment_collection')
                ->setOrderFilter($this)
                ->load();
        } else {
            return false;
        }
    }
    return $this->_shipments;
}
like image 43
Alan Storm Avatar answered Nov 11 '22 21:11

Alan Storm