Magento create shipment with tracking number programmatically
Asked Answered
C

2

6

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

Castellany answered 9/6, 2014 at 8:5 Comment(0)
C
11

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 .

Castellany answered 9/6, 2014 at 8:7 Comment(1)
Note that $shipmentId is actually the Shipment Increment IdBiskra
B
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.

Biskra answered 8/11, 2017 at 5:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.