Magento create shipment programmatically

Magento create shipment programmatically : While working with magento projects sometimes you need to create shipments programmatically. You can create shipments from order if the shipments are not created already.
Here in this tutorial we are going to explain the steps to create shipments programmatically.

Magento create shipment programmatically

Here are the steps to create shipments-

  • Load Order By Order Id Or Increment Id.
  • Check If Order Can Create Shipment.
  • Check If items can be shipped & Set Quantity For this.

Magento create shipment programmatically:

// Load by order id
//Or load by Increment Id
$order = Mage::getModel('sales/order')->loadByIncrementId($orderIncrementId);
$email = true;
$comment="Demo Shipment";
// Check if order Can Be shipped.
if ($order->canShip()) {
$qty = array();

foreach($order->getAllVisibleItems() as $item){
	$qty[$item->getId()] = $item->getQtyToShip;

	 $shipment = $order->prepareShipment($qty);
	  if ($shipment) {
		  $shipment->addComment($comment, $email && $addComment);
            try {
                $transactionSave = Mage::getModel('core/resource_transaction')
                $shipment->sendEmail($email, ($addComment ? $comment : ''));
            } catch (Mage_Core_Exception $e) {
				Mage::log("Errror While Creating Shipment...".$e->getMessage());



In the above example we have first loaded the order for which we want to create shipments. After Loading the order we have checked whether the order can be shipped or not. Then we have filtered the items quantities which can be shipped along with the item id which is used to prepare the shipment. After preparing the shipment comment and then we have added the shipment & order object to the core transaction. Once it is saved email is sent.

Note : Don’t forget to check item Quantity to ship & Order can be shipped or not.
Tip : If you the above method to create shipments observer events can be also triggered.

More Examples

Let’s have look over more example on the shipments.

Magento Create Multi Shipment for a Single Order programmatically

As we know we can create multiple shipments for a single order. We sometimes need to create multiple shipments for one order. Here is are two methods to create multiple shipments in magento.

Method 1

Magento Create Multi Shipment for a Single Order programmatically:

$order = Mage::getModel('sales/order')->load($orderId);
$shipmentItems = array(); // array items of order to make shipments.
//collect items which for which you want to make shipment.
foreach ($shipmentItems as $item) {
$shipmentItems [$item->getId()] = $item->getQuantity();
if ($order->getId() && !empty($shipmentItems) && $order->canShip()) {
$shipment = Mage::getModel('sales/service_order', $order)->prepareShipment($shipmentItems);

The above example will create the multiple shipments from the order on the basis of the items you pass.

Method 2

Sometimes method 1 does not works properly and gives data_invalid error. You can also create multiple shipments as below also –

Magento Create Multi Shipment for a Single Order programmatically:

$orderId = 100;
$order = Mage::getModel('sales/order')->load($orderId);
$convertor = Mage::getModel('sales/convert_order');
$shipment = $convertor->toShipment($order);
foreach ($order->getAllVisibleItems() as $item) {
    // here colect order items you want to ship
    if ($item->getQtyToShip() && !$item->getIsVirtual()) {
        $shipItem = $convertor->itemToShipmentItem($item);


Add Comment

📖 Read More