0

I have a csv file with 3 columns with list of (increment_id, carrier_code, tracking_number). How do I Post POST these values to existing magento 2 order using API? I have managed to load the values from csv into a data array and into $incrementid, $carrier, $tracking. i'm new to API, please be detailed as possible.

<?php
$file = fopen('track.csv', 'r', '"'); // set path to the CSV file
if ($file !== false)
{
    // add logging capability
    $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/import-update.log');
    $logger = new \Zend\Log\Logger();
    $logger->addWriter($writer);

        public function execute()
        {
        // enter the number of data fields you require the product row inside the CSV file to contain

            $required_data_fields = 3;  //number of column in csv

            while (($row = fgetcsv($this->_file, 100, ",")) !== FALSE)
            {
                $data_count = count($row);
                if ($data_count < 1) {
                    continue;
                }

                $data = array();
                //$data = array_combine($header, $row);
                $data = array($row);

                $ponumber = $row[0]; //$data['ponumber']; // column A
                if ($data_count < $required_data_fields) {
                    $logger->info("Skipping Order Number " . $ponumber . ". Not enough data to import.");
                    continue;
                }

                $shipvia = isset($row[1]) ? $row[1] : null;
                $trackingnumber =  isset($row[2]) ? $row[2] : null;

                switch ($shipvia) {
                    case "FEDEX":
                        $shipvia = "fedex";
                        break;
                    case "UPS":
                        $shipvia = "ups";
                        break;
                    case "USPS":
                        $shipvia = "usps";
                        break;
                    default:
                        $shipvia = "fedex";
                }
            }


    fclose($file); 
}
1

For Magento 2 API Shipment tracking you can use Shipment tracking repository interface.

A shipment is a delivery package that contains products. A shipment document accompanies the shipment. This document lists the products and their quantities in the delivery package.

  • Method POST /V1/shipment/

You can pass orderId carrier_code tracking_number in POST Body Data

{
  "billingAddressId": 0,
  "createdAt": "string",
  "customerId": 0,
  "emailSent": 0,
  "entityId": 0,
  "incrementId": "string",
  "orderId": 0,
  "packages": [
    {
      "extensionAttributes": {}
    }
  ],
  "shipmentStatus": 0,
  "shippingAddressId": 0,
  "shippingLabel": "string",
  "storeId": 0,
  "totalQty": 0,
  "totalWeight": 0,
  "updatedAt": "string",
  "items": [
    {
      "additionalData": "string",
      "description": "string",
      "entityId": 0,
      "name": "string",
      "parentId": 0,
      "price": 0,
      "productId": 0,
      "rowTotal": 0,
      "sku": "string",
      "weight": 0,
      "extensionAttributes": {},
      "orderItemId": 0,
      "qty": 0
    }
  ],
  "tracks": [
    {
      "orderId": 0,
      "createdAt": "string",
      "entityId": 0,
      "parentId": 0,
      "updatedAt": "string",
      "weight": 0,
      "qty": 0,
      "description": "string",
      "extensionAttributes": {},
      "trackNumber": "string",
      "title": "string",
      "carrierCode": "string"
    }
  ],
  "comments": [
    {
      "isCustomerNotified": 0,
      "parentId": 0,
      "extensionAttributes": {},
      "comment": "string",
      "isVisibleOnFront": 0,
      "createdAt": "string",
      "entityId": 0
    }
  ],
  "extensionAttributes": {}
}
  • can you show a POST example? can i post it with only these 3 information like this: { "incrementId": "string", "tracks": [ { "trackNumber": "string", "carrierCode": "string" } } – Kris Wen Apr 9 at 16:34
  • First really sorry for late reply @Kris , all data is exists including { "incrementId": "string", "tracks": [ { "trackNumber": "string", "carrierCode": "string" } } above information – Aditya Shah Apr 16 at 8:06
1

If you are shipping all items in the order, there is no need to actually include the items - this is all you need to create a shipment with tracking:

{
  "tracks": [
    {
      "track_number": "1Y-9876543210",
      "title": "Custom",
      "carrier_code": "SomeCarrierCode"
    }
  ]
}

As long as you have the Magento order ID in the API endpoint rest/V1/order/{order_id}/ship, this will work.

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.