bla0009 bla0009 - 5 months ago 19
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:


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


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

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

        } catch (Exception $e) {
            $order->addStatusHistoryComment('Could not automaticly create shipment. Exception message: '.$e->getMessage(), false);

    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




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

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


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

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