I want to add the tracking numbers only for created shipment. I tried below approach and getting an error.

We cannot create an empty shipment.

How can i add only the tracking number programmatically.

$orderNumber = '000000001';
$order = $this->orderFactory->loadByIncrementId($orderNumber);

$shipment = $this->orderModel->toShipment($order);

foreach ($order->getAllItems() AS $orderItem) {

if (!$orderItem->getQtyToShip() || $orderItem->getIsVirtual()) {
    $qtyShipped = $orderItem->getQtyToShip();

    $shipmentItem = $this->orderModel->itemToShipmentItem($orderItem)->setQty($qtyShipped);



try {

      $trackingIds = array(
         '0'=>array('carrier_code'=>'fedex','title' => 'Federal Express','number'=>'3131331230'),
         '1'=>array('carrier_code'=>'ups','title' => 'UPS Ground','number'=>'1312312')
foreach ($trackingIds as $trackingId) { 
       $data = array(
            'carrier_code' => $trackingId['carrier_code'],
             'title' => $trackingId['title'],
             'number' => $trackingId['number'],
         $track = $this->trackFactory->create()->addData($data);



// Send email
enter code here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.