0

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

0

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();
        }

}
  • Hi Thanks for your input, I still cannot get this to work... I've updated my code above. – user1704524 Jul 30 '14 at 13:53
  • are you create invoice on whole order items?? or you have doing partial invoice?? – Amit Bera Jul 30 '14 at 14:04
  • are you create invoice programmatically?? – Amit Bera Jul 30 '14 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... – user1704524 Jul 30 '14 at 14:09
  • now check again. – Amit Bera Jul 30 '14 at 14:11
1

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') {
  • 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. – user1704524 Jul 30 '14 at 13:03
  • I have updated my answer you will need to update the code as per your requirement – TBI Infotech Jul 30 '14 at 13:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.