Below is a snippet from my code. I get the following error when I try and create a shipment and track info for the order.

Uncaught exception 'Mage_Core_Exception' with message 'Cannot create an empty   shipment.' in /var/www/app/Mage.php:595
Stack trace:
#0 /var/www/app/code/core/Mage/Sales/Model/Order/Shipment.php(556):   Mage::throwException('Cannot create a...')
#1 /var/www/app/code/core/Mage/Core/Model/Abstract.php(316): Mage_Sales_Model_Order_Shipment->_beforeSave()
#2 /var/www/app/code/core/Mage/Core/Model/Resource/Transaction.php(151): Mage_Core_Model_Abstract->save()
#3 /var/dataimporters/updateorder.php(197): Mage_Core_Model_Resource_Transaction->save()
#4 {main}
thrown in /var/www/app/Mage.php on line 595

Any recommendation as to why my script is resulting in an empty shipment?

    if ($shipping['shipper'] == 'UPS') {
    $title = 'United Parcel Service';
    if ($shipping['shipper'] == 'USPS') {
    $title = 'United States Postal Service';
    if ($shipping['shipper'] == 'custom') {
    $title = 'Custom Value';

    $comment = null;
    $email = false;
    $includeComment = false;                                                    
    $order = Mage::getModel('sales/order')->loadByIncrementId($orderId);
    $convertor = Mage::getModel('sales/convert_order');
    $shipment = $convertor->toShipment($order);

    foreach ($order->getAllItems() as $orderItem)
        if (!$orderItem->getQtyToShip()) {
        if ($orderItem->getIsVirtual()) {
        $item = $convertor->itemToShipmentItem($orderItem);
        $qty = $orderItem->getQtyToShip();

    $trackdata = array();
    $trackdata['carrier_code'] = $carrier;                                  
    $trackdata['title'] = $title;                                          
    $trackdata['number'] = $trackInfo;                                       

    $track = Mage::getModel('sales/order_shipment_track')->addData($trackdata);
    //echo "<pre>"; print_r($shipment);echo "<pre>"; exit;

    Mage::register('current_shipment', $shipment);

    $shipment->addComment($comment, $email && $includeComment);

    $transactionSave = Mage::getModel('core/resource_transaction')

    $shipment->sendEmail($email, ($includeComment ? $comment : ''));

2 Answers 2


Your code works fine for me in my development environment. What you may be doing is passing an order entity_id instead of the Increment Id on this line:

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

Make sure $orderId is the order's entity_id or change $orderId to the order's Increment Id.

  • oh could be be let me check that out
    – ddpishere
    Commented Feb 7, 2015 at 16:21
  • I looked at my script and I am call ing this before that section $orderId = $order->getIncrementId();
    – ddpishere
    Commented Feb 7, 2015 at 16:38
  • The only way I could get your code to fail was to pass it an invalid ID. Commented Feb 7, 2015 at 16:57

OR you could change this following line to

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


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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.