How to create Shipment and invoice by code in magento

For automization magento we required auto create a shipment and invoice
therefor i decide to write blog for create shipment and invoice by code so after placing an order it will auto generate invoice and shipment.
Following are the steps for create a shipment and invoice.
step 1: create invoice by code

order_id = "100000042";
//load order by increment id
$order = Mage::getModel("sales/order")->loadByIncrementId($order_id);
try {
	if($order->canInvoice()) {
		//Create invoice with pending status
		$invoiceId = Mage::getModel('sales/order_invoice_api')
			->create($order->getIncrementId(), array());

		$invoice = Mage::getModel('sales/order_invoice')
						->loadByIncrementId($invoiceId);

		//set invoice status "paid"
		$invoice->capture()->save();
	}
}catch (Mage_Core_Exception $e) {
// print_r($e);
}

Step 2: Create Shipment by code

order_id = "100000042";

//load order by increment id
$order = Mage::getModel("sales/order")->loadByIncrementId($order_id);
try {
	if($order->canShip()) {
		//Create shipment
		$shipmentid = Mage::getModel('sales/order_shipment_api')
						->create($order->getIncrementId(), array());
		//Add tracking information
		$ship = Mage::getModel('sales/order_shipment_api')
						->addTrack($order->getIncrementId(), array());		
	}
}catch (Mage_Core_Exception $e) {
// print_r($e);
}

Step3 : Create cron Or setup observer for create auto invoice and shipment

One Response to “How to create Shipment and invoice by code in magento”

  1. Peter Lembke Says:

    In Magento 1.4.2.0 I get:
    invoice created, invoice paid, and state “Processing”.
    shipment created, all products shipped, state “Processing”, also get an exception on the “addTrack”.
    I will follow up the exception with a new order.


Speak Your Mind

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: