12

I did find sample codes for Magento 1.x. But I have no idea how to do this on Magento 2.

Can anyone describe how to implement this using Dependency Injection (DI)?

Thanks.

$trackingDetail = array(
    'carrier_code' => 'ups',
    'title' => 'United Parcel Service',
    'number' => 'TORD23254WERZXd3', // Replace with your tracking number
);

$track = Mage::getModel('sales/order_shipment_track')->addData($trackingDetail);
$shipment->addTrack($track);


$transactionSave = Mage::getModel('core/resource_transaction')
->addObject($shipment)
->addObject($shipment->getOrder())
->save();
15

AFAIK the track object is the same in M2.

However, the rest of the code has changed.

$data = array(
    'carrier_code' => 'ups',
    'title' => 'United Parcel Service',
    'number' => 'TORD23254WERZXd3', // Replace with your tracking number
);

$track = $this->trackFactory->create()->addData($data);
$shipment->addTrack($track)->save();

Where $this->trackFactory an instance of Magento\Sales\Model\Order\Shipment\TrackFactory and $shipment is your shipment object.

6
  • BTW I wonder if you know how to get order shipment comments too for MG2. If you do please post it here. Thanks. Jan 25 '17 at 6:50
  • 1
    How to add tracking URl in shipment? Feb 7 '18 at 5:53
  • @Roshanruzaik, if you are asking how to add comment to the shipment then this is how i have done:$commentText = 'Test comment'; //Comment interface : \Magento\Sales\Api\Data\ShipmentCommentCreationInterface $comment= $this->commentInterface->setComment($commentText); //use $comment as one of the paramter while creating shipment as below example: //ship order service: \Magento\Sales\Model\ShipOrder $this->shipOrderService->execute($orderId,$shippedItems,$notify,$includeComment,$comment,$tracks); Jun 4 '18 at 23:16
  • Hi @Raphael at digital pianism sipping tracking number, and carrier_code is saving in "sales_shipment_track" table in magento2 but where it will save the Shipping Charge ?
    – Nagaraju K
    Nov 19 '18 at 11:55
  • can you please help on this similar question magento.stackexchange.com/questions/268844/… Apr 5 '19 at 19:38
11

Adding to the accepted answer, it would be a good idea to use repositories instead of the deprecated save method. Also added the customer notification after the tracking creation.

/** @var Magento\Sales\Model\Order\ShipmentRepository */
protected $_shipmentRepository;

/** @var Magento\Shipping\Model\ShipmentNotifier */
protected $_shipmentNotifier;

/** @var Magento\Sales\Model\Order\Shipment\TrackFactory */
protected $_trackFactory; //missing ;

public function __construct(
  \Magento\Shipping\Model\ShipmentNotifier $shipmentNotifier, 
  \Magento\Sales\Model\Order\ShipmentRepository $shipmentRepository, 
  \Magento\Sales\Model\Order\Shipment\TrackFactory $trackFactory)
{
  $this->_shipmentNotifier = $shipmentNotifier;
  $this->_shipmentRepository = $shipmentRepository;
  $this->_trackFactory = $trackFactory;
}
public function addTrack($shipment, $carrierCode, $description, $trackingNumber) 
{
    /** Creating Tracking */
    /** @var Track $track */
    $track = $this->_trackFactory->create();
    $track->setCarrierCode($carrierCode);
    $track->setDescription($description);
    $track->setTrackNumber($trackingNumber);
    $shipment->addTrack($track);
    $this->_shipmentRepository->save($shipment);

    /* Notify the customer*/
    $this->_shipmentNotifier->notify($shipment);
 }

Where $shipment is your shipment object. Notify will notify (send email) to the user and add a history item to the order status history collection.

3
  • It did not work for me have created helper class , i am able to fetch getDeiveryTime(), getCancelTime(),getDispatchTime() but i am unable to get tracking number. Can you provide a list of functions to fetch rest details of the shipment Mar 15 '19 at 22:54
  • can you help on this similar question? magento.stackexchange.com/questions/268844/… Apr 5 '19 at 19:24
  • @carol Castro, how can we add the order id here?
    – Mujahidh
    Sep 26 '19 at 14:19

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.