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:

if($order->canShip())
{
$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.