Bob
Bob

Reputation: 8714

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!

Upvotes: 1

Views: 2512

Answers (3)

Dennis van Schaik
Dennis van Schaik

Reputation: 474

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.

Upvotes: 1

user2729065
user2729065

Reputation: 1

To put code in a template is a really bad idea: it adds the Shipping code to your Template / View. This functionality doesn't belong there. (For instance: if the payment is confirmed after 5 minutes it doesn't work).

Solution is to write an Observer to the Payment-Success event: http://inchoo.net/ecommerce/magento/magento-orders/automatically-invoice-ship-complete-order-in-magento/

Upvotes: 0

Bob
Bob

Reputation: 8714

I found a solution. I guess this is not the best way to do it, but it works.

In file your_theme_name/template/checkout/success.phtml

add this code

<?php
$orderId = Mage::getSingleton('checkout/session')->getLastRealOrderId();
$order = Mage::getModel('sales/order') -> loadByIncrementId($orderId);

if ($order -> canShip()) {
    $itemQty = $order -> getItemsCollection() -> count();
    $shipment = Mage::getModel('sales/service_order', $order) -> prepareShipment($itemQty);
    $shipment = new Mage_Sales_Model_Order_Shipment_Api();
    $shipmentId = $shipment -> create($orderId);
}
?>

That will add shipping for the order on receipt page.

Upvotes: 0

Related Questions