0

I am trying to create an Observer to automatically create and email shipment to the customer whenever a new invoice is created.

I am having trouble created the shipment as the I keep getting an exception when I try to create an invoice.

I am Observer the sales_order_invoice_register register event.

Here's my code:

public function salesOrderInvoiceShipmentCreate($observer)
{

    $orderIncrementId = Mage::getSingleton('checkout/session')->getLastRealOrderId();

    $order = Mage::getModel('sales/order')->loadByIncrementId($orderIncrementId);
    Mage::log($order,Zend_Log::INFO,'order.log',true);

    if (!$order->getId()) {
        Mage::throwException("Order does not exist, for the Shipment process to complete");
    }

    if ($order->canShip()) {
        try {
            $shipment = Mage::getModel('sales/service_order', $order)
                ->prepareShipment($this->_getItemQtys($order));

            $shipmentCarrierCode = '';
            $shipmentCarrierTitle = '';

            $arrTracking = array(
                'carrier_code' => isset($shipmentCarrierCode) ? $shipmentCarrierCode : $order->getShippingCarrier()->getCarrierCode(),
                'title' => isset($shipmentCarrierTitle) ? $shipmentCarrierTitle : $order->getShippingCarrier()->getConfigData('title'),
            );

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

            // Register Shipment
            $shipment->register();

            // Save the Shipment
            $this->_saveShipment($shipment, $order, $customerEmailComments);

            // Finally, Save the Order
            $this->_saveOrder($order);
        } catch (Exception $e) {
            throw $e;
        }
    }
}

protected function _getItemQtys(Mage_Sales_Model_Order $order)
{
    $qty = array();

    foreach ($order->getAllItems() as $_eachItem) {
        if ($_eachItem->getParentItemId()) {
            $qty[$_eachItem->getParentItemId()] = $_eachItem->getQtyOrdered();
        } else {
            $qty[$_eachItem->getId()] = $_eachItem->getQtyOrdered();
        }
    }

    return $qty;
}

The exception is being thrown on but I can't figure out why??

Any help would be appreciated please...

2
  • please add the event name that you are observing to the question Commented Sep 12, 2014 at 16:15
  • @programmer_rkt Thanks for you input, please see my edit Commented Sep 12, 2014 at 16:20

1 Answer 1

1

The LastRealOrderId value on the checkout\session object is cleared when the order is saved. See: Mage_Checkout_Model_Type_Onepage::saveOrder()

The order is already available from the $observer object that is passed for that event, see: Mage_Sales_Model_Order_invoice::register() where the event is being generated.

Try:

$order = $observer->getEvent()->getOrder();

In your function instead of the LastRealOrderId

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.