How to make order shipment from code

How to make order shipment from code

As most of you know, there is an Order interface in Magento administration from where you can trigger order shipping. However, while developing certain module, you might wish to trigger this event from the code. I hope that this code snippet will help you in this process. In my case I have used this code to make shipment from cron after I got confirmation from fulfillment center
We will use Magento API to achieve it.

First, load order. I’ll use load “loadByIncrementId” method.

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

Now I let’s make shipment:

$itemQty =  $order->getItemsCollection()->count();
$shipment = Mage::getModel('sales/service_order', $order)->prepareShipment($itemQty);
$shipment = new Mage_Sales_Model_Order_Shipment_Api();
$shipmentId = $shipment->create($orderId);

Order is now complete and order items statuses are “mixed”-> invoiced / shipped:

I think, code is quite self-explanatory, and I hope you will find it usefull, however,
remember not to use it on production sites without further testing.

You made it all the way down here so you must have enjoyed this post! You may also like:

Making FedEx api show shipping estimate Toni Pap
Toni Pap, | 1

Making FedEx api show shipping estimate

When can you deliver? (FedEx) Davor Simek
Davor Simek, | 8

When can you deliver? (FedEx)

Sorry, we can’t ship there Sasa Brankovic
Sasa Brankovic, | 12

Sorry, we can’t ship there


  1. “Hello. It s a very interesting idea to create such extension. Well by the way, I find another way to do such functional. But it can be inconvenient ?? some cases.

    First of all I use extension for adding some attributes to the order.

    And I add attribute “”Delivery Date”” on Shipping section on my checkout page. Of course there are no any restrictions and rules. Customer can just set a date which will suite him for delivery

    I now, it sounds foolishly but it helps me 🙂

    Thank you


  2. Just saved me hours! Thanks!

    For the people looking to add tracking you can use the following after you have run line 6 above:

    $shipment->addTrack($shipmentId, 'usps', 'Tracking ID', $trackinNumber);

    Where ‘usps’ is the shipping method used (note this must be a method magneto already knows about) and ‘Tracking number’ is any descriptive text you want to use.

    Like wise this will add a comment (the fase on the end prevents emails from being sent out)

    $shipment->addComment($shipmentId, $shippingNotes, false, false);

    Hopefully someone will find that useful too!

    – Oliver

  3. The last line “$shipmentId = $shipment->create($orderId);” leads to an error: There has been an error processing your request: order_not_exists

    Is the OrderID the right variable the API wants?

  4. Hi,
    I was reading up on the last three articles on how you do the following:
    “Programmatically create order in Magento”
    “How to make order shipment from code”
    “How to extend Magento Order Grid?”
    My dilemma is I am trying to create the following my already existing Delivery Date column which displays delivery date that is generated by by shipping method as attribute to populate in a new column called “Ship-Date” Now Ship Date needs to have a different set a rules which I wrote so a quick example of that would be if Next was ordered on Monday than is “Ship-Date” is Monday to be delived on Tuesday Not the greatest example there are other variables like Business days weekends 2 day shipping holidays no delivery monday’s etc but sort of an idea of what I was trying to accomplish….Do you have any thoughts?

  5. Hi there, thank for the great post. Pardon me for asking if you have a download for this feature? Because I do not know why to put these codes and how to activate it using cron. Your reply is very much appreciated, thanks in advance.

  6. What is the use of affecting “Mage::getModel(‘sales/service_order’, $order)->prepareShipment($itemQty);” to “$shipment” and just after affecting to the SAME variable a “new Mage_Sales_Model_Order_Shipment_Api();” ?

    The “prepared shipment” looks not to be used…

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <blockquote cite=""> <code> <del datetime=""> <em> <s> <strike> <strong>. You may use following syntax for source code: <pre><code>$current = "Inchoo";</code></pre>.

Tell us about your project

Drop us a line. We'd love to know more about your project.