1

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.

order_no,sku,quantity,tracking_no

700100,JK5012,1,132131315

700100,KM5013,2,132131316

Can anyone help us with the above requirement?

Your help will be appreciated.

0

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();
    $track->setCarrierCode($carrierCode);
    $track->setDescription($description);
    $track->setTrackNumber($trackingNumber);
    $shipment->addTrack($track);
    $this->_shipmentRepository->save($shipment);
$this->_shipmentNotifier->notify($shipment);

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? – Emipro Technologies Pvt. Ltd. Apr 10 '18 at 12:50
  • yes, you can implement it with cron as well – John Apr 12 '18 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/… – Kris Wen Apr 5 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. – Kris Wen Apr 5 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.