bla0009 bla0009 - 1 year ago 43
PHP Question

How to create shipping automatically in Magento

I need to change Magento default workflow. So, I should automatically create shipping as soon as customers buy something.(when customers see Receipt page). I am not sure where should I start. I started googling for some extension, but no luck for now. That's why I came here. Does anyone have an idea where can I start resolving this problem? Thanks!

Answer Source

You should never put this kind of code in view files. Besides it being bad practice in general, as @user2729065 mentioned: if the customer does not return to the thank you page after the payment, the code will not be run. Better is to create a custom module with an observer. To do this, add the following code to your module etc/config.xml file:

<global>
    <events>
        <sales_order_invoice_pay>
            <observers>
                <[my]_[module]_automatically_complete_order>
                    <class>[module]/observer</class>
                    <method>automaticallyShipCompleteOrder</method>
                </[my]_[module]_automatically_complete_order>
            </observers>
        </sales_order_invoice_pay>
    </events>
</global>

Change my_module to your module name. This will triger when an invoice is paid.

Then create the Observer in My/Module/Model/Observer.php

<?php

class My_Module_Model_Observer
{
/**
 * Mage::dispatchEvent($this->_eventPrefix.'_save_after', $this->_getEventData());
 * protected $_eventPrefix = 'sales_order';
 * protected $_eventObject = 'order';
 * event: sales_order_invoice_pay
 */
public function automaticallyShipCompleteOrder($observer)
{
    $order = $observer->getEvent()->getInvoice()->getOrder();

    if ($order->getState() == Mage_Sales_Model_Order::STATE_PROCESSING) {

        try {
            $shipment = $order->prepareShipment();
            $shipment->register();

            $order->setIsInProcess(true);
            $order->addStatusHistoryComment('Shippment automatically created.', false);

             Mage::getModel('core/resource_transaction')
                ->addObject($shipment)
                ->addObject($shipment->getOrder())
                ->save();
        } catch (Exception $e) {
            $order->addStatusHistoryComment('Could not automaticly create shipment. Exception message: '.$e->getMessage(), false);
            $order->save();
        }
    }

    return $this;
}

}

This will check if the order is still in Processing ( the state it gets after it is paid for.). And if so, try to create the shipment. After the shipment is created, the order will automatically change its state to completed.

If you want the shipment to get created directly after the order is placed (before the invoice is created and paid for.), change

<sales_order_invoice_pay> 

in

<sales_order_place_after> 

in config.xml. And because this observer returns an order and not an invoice, also change:

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

to

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

Code is based on an example from Inchoo so most credits go to them.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download