I am trying to automatically send shipment email when an invoice is created for an order but I can't get me code to work...

Here is what I have:


<?xml version="1.0"?>

And Observer.php


class Namespace_Invoicer_Model_Observer

    public function implementOrderStatus ($event)
        $order = $event->getOrder ();

        if ($this->_getPaymentMethod ($order) == 'ccsave') {
            if ($order->canInvoice ())
                $this->_processOrderStatus ($order);
        return $this;

    private function _getPaymentMethod ($order)
        return $order->getPayment ()->getMethodInstance ()->getCode ();

    private function _processOrderStatus ($order)
        $invoice = $order->prepareInvoice ();

        $invoice->register ();
        Mage::getModel ('core/resource_transaction')
            ->addObject ($invoice)
            ->addObject ($invoice->getOrder ())
            ->save ();

        $invoice->sendEmail (true, '');
        $this->_changeOrderStatus ($order);
        return true;

    private function _changeOrderStatus ($order)
        $statusMessage = 'Order has been updated';
        $order->setState (Mage_Sales_Model_Order::STATE_COMPLETE, true);
        $order->save ();


I did follow this example from Inchoo if anyone has been able to get this to work please let me as I'm banging my head off of the desk...

2 Answers 2


As you said ,you want to send shipment mail after create Invoice.
For this ,you need change the event from sales_order_save_after to sales_order_invoice_register which is better ...

See more at


Run method after capturing invoice

Also Some issue in config.xml


Should be from Namespace_Invoicer_Model_Observer to invoicer/observer Also Observer file implementOrderStatus() , $Order have wrong parameter

public function implementOrderStatus ($Observer)
    $order =$Observer->getEvent()->getOrder ();

    if ($this->_getPaymentMethod ($order) == 'ccsave') {

            $this->_processOrderStatus ($order->getIncrementId());

    return $this;

For Doing shipment on Order all items try below code ,so processOrderStatus() code should be

public function processOrderStatus($orderIncrementId){
$itemsQty = array();
$comment = null;
$email = true;
$includeComment = false;

$order = Mage::getModel('sales/order')->loadByIncrementId($orderIncrementId);
if ($order->canShip()){

$itemsQty = array();
foreach ($order->getAllItems() as $item) {
    $itemsQty[$item->getId()] = $item->getQtyOrdered();

     $shipment = $order->prepareShipment($itemsQty);

     $shipment = $order->prepareShipment($itemsQty);

        if ($shipment) {
            $shipment->addComment($comment, $email && $includeComment);
            if ($email) {
            try {
                $transactionSave = Mage::getModel('core/resource_transaction')
                $shipment->sendEmail($email, ($includeComment ? $comment : ''));
            } catch (Mage_Core_Exception $e) {
                var_dump( $e->getMessage());
           echo $shipment->getIncrementId();

  • Hi Thanks for your input, I still cannot get this to work... I've updated my code above. Jul 30, 2014 at 13:53
  • are you create invoice on whole order items?? or you have doing partial invoice??
    – Amit Bera
    Jul 30, 2014 at 14:04
  • are you create invoice programmatically??
    – Amit Bera
    Jul 30, 2014 at 14:05
  • At the moment I am creating the Invoice through Admin, What I would like to do is when an invoice is created also create shipment at the same time and email to the customer... Jul 30, 2014 at 14:09
  • now check again.
    – Amit Bera
    Jul 30, 2014 at 14:11

Try this code:

class Namespace_Module_Model_Observer
public function implementOrderStatus($event)
    $order = $event->getOrder();

    if ($this->_getPaymentMethod($order) == 'ccsave') {
        if ($order->canInvoice())
    return $this;

private function _getPaymentMethod($order)
    return $order->getPayment()->getMethodInstance()->getCode();

private function _processOrderStatus($order)
    $invoice = $order->prepareInvoice();


    $invoice->sendEmail(true, '');
    return true;

private function _changeOrderStatus($order)
    $statusMessage = '';
    $order->setState(Mage_Sales_Model_Order::STATE_PROCESSING, true);       

This is the section where you can change the code based on payment method:

For now its working for ccsave

if ($this->_getPaymentMethod($order) == 'ccsave') {
  • Thanks for your replay, I've added your code but the Shipment is still not being generated. FYI I am using PayPal and WorldPay as Payment method... See updated code. Jul 30, 2014 at 13:03
  • I have updated my answer you will need to update the code as per your requirement Jul 30, 2014 at 13:06

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.