Question

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:

config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Modulename_Invoicer>
            <version>0.1.0</version>
        </Modulename_Invoicer>
    </modules>
    <global>
        <models>
            <invoicer>
                <class>Modulename_Invoicer_Model</class>
            </invoicer>
        </models>
        <events>
            <sales_order_save_after> 
                <observers>
                    <sales_order_save_after_handler> 
                        <type>model</type> 
                        <class>invoicer/observer</class> 
                        <method>implementOrderStatus</method>  
                    </sales_order_save_after_handler>
                </observers>
            </sales_order_save_after>
        </events>
    </global>
</config> 

And Observer.php

<?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...

Was it helpful?

Solution

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

https://stackoverflow.com/questions/7661455/whats-the-event-name-when-you-create-a-new-invoice-in-magento

Run method after capturing invoice

Also Some issue in config.xml

<class>Namespace_Invoicer_Model_Observer</class>

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->register();
            $shipment->addComment($comment, $email && $includeComment);
            if ($email) {
                $shipment->setEmailSent(true);
            }
            $shipment->getOrder()->setIsInProcess(true);
            try {
                $transactionSave = Mage::getModel('core/resource_transaction')
                    ->addObject($shipment)
                    ->addObject($shipment->getOrder())
                    ->save();
                $shipment->sendEmail($email, ($includeComment ? $comment : ''));
            } catch (Mage_Core_Exception $e) {
                var_dump( $e->getMessage());
            }
           echo $shipment->getIncrementId();
        }

}

OTHER TIPS

Try this code:

class Namespace_Module_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->setState(Mage_Sales_Model_Order::STATE_PROCESSING, true);       
$order->save();
}
}

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') {
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top