How to make order shipment from code

Featured Image

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.

About Tomas Novoselic

Team Leader/Backend Developer

At Inchoo, Tomas is a Team Leader and Certified Magento Developer who handles Magento modifications at any level. He also works closely with clients on Magento projects of any size and difficulty.

Read more posts by Tomas / Visit Tomas's profile


  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>.