Magento

Programmatically create Shipment of a new order in magento

Programmatically create Shipment of a new order in magento

Programmatically create Shipment of a new order in magento

For some time need to create Shipment of an order.I have write code for how to create Shipment of an order pragmatically here

  1. First of all load an order by order id,
  2. Check all item of an order are already creating Shipment.
  3. if ,invoice is not created then create Shipment of all items

Step1 ::load order by order id

$order=Mage::getModel(‘sales/order’)->load($orderId);
Or
$order = Mage::getModel(‘sales/order’)->loadByIncrementId($orderIncrementId);

Step2: check a item is available for shipment and set quantity for shipment

$qty=array();

$Itemqty = $eachOrderItem->getQtyOrdered()
– $eachOrderItem->getQtyShipped()
– $eachOrderItem->getQtyRefunded()
– $eachOrderItem->getQtyCanceled();
$qty[$eachOrderItem->getId()]=$Itemqty;
Step3:create a shipment and check order is available for shipment

 

if ($order->canShip()) {
/* @var $shipment Mage_Sales_Model_Order_Shipment */
/* prepare to create shipment */
$shipment = $order->prepareShipment($qty);
if ($shipment) {
$shipment->register();
$shipment->addComment($comment, $email && $includeComment);
$shipment->getOrder()->setIsInProcess(true);
try {
$transactionSave = Mage::getModel(‘core/resource_transaction’)
->addObject($shipment)
->addObject($shipment->getOrder())
->save();
$shipment->sendEmail($email, ($includeComment ? $comment : ”));
} catch (Mage_Core_Exception $e) {
var_dump($e);
}

}

}

Full Code:

$order=Mage::getModel('sales/order')->load($orderId);
//Or 
$order = Mage::getModel('sales/order')->loadByIncrementId($orderIncrementId);

$qty=array();
foreach($order->getAllItems() as $eachOrderItem){

	$Itemqty=0;
	 $Itemqty = $eachOrderItem->getQtyOrdered()
            - $eachOrderItem->getQtyShipped()
            - $eachOrderItem->getQtyRefunded()
            - $eachOrderItem->getQtyCanceled();
	$qty[$eachOrderItem->getId()]=$Itemqty;

}

/*
echo "<pre>";
print_r($qty);
echo "</pre>";
*/
/* check order shipment is prossiable or not */

$email=true;
$includeComment=true;
$comment="test Shipment";

if ($order->canShip()) {
         /* @var $shipment Mage_Sales_Model_Order_Shipment */
		/* prepare to create shipment */
	 $shipment = $order->prepareShipment($qty);
	  if ($shipment) {
		  $shipment->register();
		  $shipment->addComment($comment, $email && $includeComment);
		  $shipment->getOrder()->setIsInProcess(true);
            try {
                $transactionSave = Mage::getModel('core/resource_transaction')
                    ->addObject($shipment)
                    ->addObject($shipment->getOrder())
                    ->save();
                $shipment->sendEmail($email, ($includeComment ? $comment : ''));
            } catch (Mage_Core_Exception $e) {
				var_dump($e);
            }

	  }

}

Item wise Order shipment:

This code is create a shipment of an all items of an order.If you want to create product wise
Then follow below code

$qty=array();
$eachOrderItem = Mage::getModel('sales/order_item')->load($itemId);

	$Itemqty=0;
	 $Itemqty = $eachOrderItem->getQtyOrdered()
            - $eachOrderItem->getQtyShipped()
            - $eachOrderItem->getQtyRefunded()
            - $eachOrderItem->getQtyCanceled();
	$qty[$eachOrderItem->getId()]=$Itemqty;
$email=true;
$includeComment=true;
$comment="test Shipment";

if ($order->canShip()) {
         /* @var $shipment Mage_Sales_Model_Order_Shipment */
		/* prepare to create shipment */
	 $shipment = $order->prepareShipment($qty);
	  if ($shipment) {
		  $shipment->register();
		  $shipment->addComment($comment, $email && $includeComment);
		  $shipment->getOrder()->setIsInProcess(true);
            try {
                $transactionSave = Mage::getModel('core/resource_transaction')
                    ->addObject($shipment)
                    ->addObject($shipment->getOrder())
                    ->save();
                $shipment->sendEmail($email, ($includeComment ? $comment : ''));
            } catch (Mage_Core_Exception $e) {
				var_dump($e);
            }

	  }

}