We have an intermittent 'Cannot create an empty shipment.' when bulk processing shipments. Used the API before.
This controller is triggered with bulk selected orders in order overview. It ships all items per order and sends the e-mail.
Somehow we get 'Cannot create an empty shipment.' …. $itemQty > 0 just checked
Are we forgetting anything?
public function _shipmailinvoice($email=true) {
$orderIds = $this->getRequest()->getPost('order_ids', array());
$cnt_Orders = count($orderIds);
$cnt_Shipments = 0;
$cnt_Invoices = 0;
if (!empty($orderIds)) {
foreach ($orderIds as $orderId) {
$order = Mage::getModel('sales/order')->load($orderId);
//$order = Mage::getModel('sales/order')->loadByIncrementId($orderId);
$itemQty = (int)$order->getItemsCollection()->count();
$shipment = $order->prepareShipment($itemQty);
//$shipment = Mage::getModel('sales/service_order', $order)->prepareShipment($itemQty);
if ($shipment && ($order->hasShipments() < 1)) {
$shipment->register();
$order->setIsInProcess(true);
$order->addStatusHistoryComment('Shipment created by SNH_SHipMailInvoice.', false);
try {
$transactionSave = Mage::getModel('core/resource_transaction')
->addObject($shipment)
->addObject($shipment->getOrder())
->save();
if ($email) { $shipment->sendEmail($email, '')->setEmailSent(true)->save(); }
$cnt_Shipments++;
} catch (Mage_Core_Exception $e) {
// var_dump($e);
$this->_getSession()->addError($e, 'Cannot create shipment');
}
} else {
if ($email) { $shipment->sendEmail($email, '')->setEmailSent(true)->save(); }
$cnt_Shipments++;
}`
Best Answer
This worked for us