4

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';
    }

     Mage::init();
    $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()) {
            continue;
        }
        if ($orderItem->getIsVirtual()) {
            continue;
        }
        $item = $convertor->itemToShipmentItem($orderItem);
        $qty = $orderItem->getQtyToShip();
        $item->setQty($qty);
        $shipment->addItem($item);
    }

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



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

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

    $shipment->register();
    $shipment->addComment($comment, $email && $includeComment);
    $shipment->setEmailSent(true);
    $shipment->getOrder()->setIsInProcess(true);


    $transactionSave = Mage::getModel('core/resource_transaction')
        ->addObject($shipment)
        ->addObject($shipment->getOrder())
        ->save();

    $shipment->sendEmail($email, ($includeComment ? $comment : ''));
}
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.

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

OR you could change this following line to

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

to

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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