Magento – Unable to create shipment programmatically

magento-1.9shipmentshipment-tracking

I try to create programmatically a shipment of an order with a track code.
This is my current code :

$order = Mage::getModel('sales/order')->loadByIncrementId("100014930");
$shipment = Mage::getModel('sales/service_order', $order)->prepareShipment($order->getItemQtys());

$arrTracking = array(
                'carrier_code' => $order->getShippingCarrier()->getCarrierCode(),
                'title' => $order->getShippingCarrier()->getConfigData('title'),
                'number' => "1234",
            );

$track = Mage::getModel('sales/order_shipment_track')->addData($arrTracking);
$shipment->addTrack($track);

$shipment->register();
$order->save(); 

With this, I have no error while I execute it.
In the Magento panel, you can't ship the order, but the order stay in pending and the shipment doesn't appear.
Please help

Problem solved with this solution

Best Answer

I think I've found your error. getItemQtys is not a valid function on any Magento class, so in Magento magic methods it returns null. Essentially you're trying to ship null number of items. You can't do that. Yes, it fails silently.

Some controllers implement a _getItemQtys method to return the values you're looking for; however none of these are plug and play. Instead I suggest you create this as it's just a keyed array in the format of:

array(
  'order_item_id'=>$qty,
  'order_item_id'=>$qty,
  'order_item_id'=>$qty
)

However, if you're always shipping all items in a shipment, without exception there is an even easier way:

$converter=Mage::getModel('sales/convert_order');
$shipment=$converter->toShipment($order);

Please read this answer here for more information on how to create shipments programmatically:

https://magento.stackexchange.com/a/3345/336