Question

I have created an Observer in order to create and email shipment when an invoice is created.

I am using the sales_order_invoice_register event to create the shipment but for some reason I cannot get the email to send...

Here is my code:

public function salesOrderInvoiceShipmentCreate($observer)
{
    $shipmentCarrierTitle = $shipmentCarrierCode;
    $customerEmailComments = '';
    $order = $observer->getEvent()->getOrder();

    if (!$order->getId()) {
        Mage::throwException("Order does not exist, for the Shipment process to complete");
    }
    try {
        $shipment = Mage::getModel('sales/service_order', $order)->prepareShipment(_getItemQtys($order));

        $arrTracking = array(
            'carrier_code' => isset($shipmentCarrierCode) ? $shipmentCarrierCode : $order->getShippingCarrier()->getCarrierCode(),
            'title' => isset($shipmentCarrierTitle) ? $shipmentCarrierTitle : $order->getShippingCarrier()->getConfigData('title'),
            'number' => $shipmentTrackingNumber,
        );
        $track = Mage::getModel('sales/order_shipment_track')->addData($arrTracking);
        $shipment->addTrack($track);
        $shipment->register();

        $shipment->save();
        $track->save();
        $email=true;
        $shipment->sendEmail($email,’’);
        _saveShipment($shipment, $order, $customerEmailComments);
    }catch (Exception $e) {
        throw $e;
    }
    return $save;
}
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;
}

protected function _saveShipment(Mage_Sales_Model_Order_Shipment $shipment, Mage_Sales_Model_Order $order, $customerEmailComments = '')
{
    $shipment->getOrder()->setIsInProcess(true);
    Mage::log($shipment->debug(),Zend_Log::INFO,'shipment.log',true);
    $transactionSave = Mage::getModel('core/resource_transaction')
        ->addObject($shipment)
        ->addObject($order)
        ->save();

    $emailSentStatus = $shipment->getData('email_sent');
    if($emailSentStatus)
        Mage::log("Email has been sent",Zend_Log::INFO,'email.log',true);
    else
        Mage::log("IS FALSE",Zend_Log::INFO,'email.log',true);
    if (!is_null($shipment->getOrder()->getCustomerEmail()) && !$emailSentStatus) {
        $shipment->setEmailSent(true);
        $shipment->sendEmail(true, $customerEmailComments);
    }

    return $this;
}

protected function _saveOrder(Mage_Sales_Model_Order $order)
{
    $order->setData('state', Mage_Sales_Model_Order::STATE_COMPLETE);
    $order->setData('status', Mage_Sales_Model_Order::STATE_COMPLETE);
    $order->save();
    return $this;
}

The Shipment is created fine but the shipment email is not sent can anyone help?

Was it helpful?

Solution

They seem to be few issues within your code

  1. Undefined variable (minor)

    • $shipmentCarrierTitle = $shipmentCarrierCode;
  2. Need to use $this-> prefix when calling a method

$shipment = Mage::getModel('sales/service_order', $order)->prepareShipment(_getItemQtys($order));

_saveShipment($shipment, $order, $customerEmailComments);

change to $this->_getItemQtys($order) and $this->_saveShipment(..

Also take a look at Automatically invoice/ship/complete order in Magento and Magento How do I automatically add tracking numbers to an order

//START Handle Shipment
$shipment = $order->prepareShipment();
$shipment->register();

$order->setIsInProcess(true);
$order->addStatusHistoryComment('Automatically SHIPPED by Inchoo_Invoicer.', false);

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

 $track = Mage::getModel('sales/order_shipment_track')
             ->setShipment($shipment)
             ->setData('title', 'Citylink')
             ->setData('number', $track_no)
             ->setData('carrier_code', 'custom')
             ->setData('order_id', $shipment->getData('order_id'))
             ->save();
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top