Programming – Intermittent ‘Cannot Create an Empty Shipment’ When Bulk Processing Shipments

codeprogramming

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

$orderIds = $this->getRequest()->getPost('order_ids', array());

    $cnt_Orders     = count($orderIds);
    $cnt_Shipments  = 0;
    $cnt_Invoices   = 0;

    if (!empty($orderIds)) {
        foreach ($orderIds as $orderId) {
            if (!$ship) continue;
            $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);
            $shipment = $order->prepareShipment();
            if ($shipment && $order->canShip()) {
                $shipment->register();
                if ($email) $shipment->setEmailSent($email);
                $shipment->getOrder()->setIsInProcess(true);
                try {
                    $transactionSave = Mage::getModel('core/resource_transaction')
                    ->addObject($shipment)
                    ->addObject($shipment->getOrder())
                    ->save();
                    if ($email) $shipment->sendEmail($email, '');
                    $cnt_Shipments++;
                } catch (Mage_Core_Exception $e) {
                    $this->_getSession()->addError($e, 'Cannot create shipment');
                }
                unset($shipment);
            } else {
                if ($email) { $shipment->sendEmail($email, '')->setEmailSent(true)->save(); }
                $cnt_Shipments++;
            }

            if ($cnt_Shipments > 0 && $cnt_Shipments < $cnt_Orders) {
             $this->_getSession()->addNotice(Mage::helper('sales')->__('Sent %s shipments and notications of %s requested. Not all shipments were sent.', $cnt_Shipments, $cnt_Orders));
            }

    }