We need to create programmatically shipment and add tracking number for all the ordered items from CSV.

Check following CSV sample data for your reference.




Can anyone help us with the above requirement?

Your help will be appreciated.

1 Answer 1


To do this, you can use trackfactory of magento 2 and within it's execute function you can get data from your csv file for example order number and sku and underneath it within foreach loop, you can add these lines

$track = $this->_trackFactory->create();

this is how your file looks like

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

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

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

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 execute(){
//your code here for getting data from csv and pass it to foreach loop to add tracking number to order from csv file

Hope this helps

  • Is your solution working with cron? Apr 10, 2018 at 12:50
  • yes, you can implement it with cron as well
    – John
    Apr 12, 2018 at 13:22
  • @John can you help out on this similar question? It will be one tracking per order. (My csv is order# | carrier | tracking) Thanks in advance. magento.stackexchange.com/questions/268844/… Apr 5, 2019 at 17:14
  • @EmiproTechnologiesPvt.Ltd. do you have the final code for your question? I might be able to use as a reference, thank you. Apr 5, 2019 at 17:57

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.