6

From a specific order I want to create shipment of that order and also allot tracking number to it programmatically. Please help . Thanks

Nikhil_K_R
  • 2,383
  • 5
  • 27
  • 51

2 Answers2

10

The question was just for knowledge sharing purpose .

One can understand some points from Ref_Link

// $order_id = Order ID
$_order = Mage::getModel('sales/order')->load($order_id);

if($_order->canShip())
{           
    $shipmentId = Mage::getModel('sales/order_shipment_api')->create($_order->getIncrementId(), $itemsarray ,'your_comment' ,false,1);
    echo $shipmentId;   // Outputs Shipment Increment Number
    $trackmodel = Mage::getModel('sales/order_shipment_api')
    ->addTrack($shipmentId,'your_shipping_carrier_code','your_shipping_carrier_title','carrier_tracking_number');
}

$itemsarray = format explained here Ref_link
Thats it !
A simple code snippet .
Hope it helps someone .

Community
  • 1
  • 1
Nikhil_K_R
  • 2,383
  • 5
  • 27
  • 51
1

The accepted answer is correct and worked for me on CE 1.9, but I wanted to expand on it.

You don't need to bother with the $itemsQty parameter, you can pass an empty array(), or leave it out altogether. It's an optional parameter, and the method prepareShipment() in app\code\core\Mage\Sales\Model\Service\Order.php will check for that data and perform a lookup if necessary.

If you want to include the tracking number in the shipment email, make sure you add the tracking first and then use Mage::getModel('sales/order_shipment_api')->sendInfo($shipmentIncrementId).

Code Snippet:

        $shipmentApi = Mage::getModel('sales/order_shipment_api');

        //pass false for email, unless you want Magento to send the shipment email without any tracking info
        //could also be written as $shipmentIncrementId = $shipmentApi->create($order->getIncrementId());
        $shipmentIncrementId = $shipmentApi->create($order->getIncrementId(), array(), '' , false, 0); 

        //add tracking info ($shippingCarrier is case sensitive)
        $shipmentApi->addTrack($shipmentIncrementId, $shippingCarrier, $shippingTitle, $trackingNumber);

        //send shipment email with tracking info
        $shipmentApi->sendInfo($shipmentIncrementId);

create() method signature:

public function create($orderIncrementId, $itemsQty = array(), $comment = null, 
                                               $email = false, $includeComment = false)

See app\code\core\Mage\Sales\Model\Order\Shipment\Api.php for all methods.

kmdsax
  • 1,361
  • 9
  • 12