In magento, how to add shipment and track number to order

In magento, how to add shipment and track number to order

September 23, 2026 · Illustrated author avatarBy Stefan Bogdanescu
In magento, how to add shipment and track number to order

Adding Shipment and Track Numbers to Magento Orders Dynamically

As an experienced developer working with e-commerce platforms like Magento, I understand the importance of streamlining order management processes for improved efficiency. In this blog post, we’ll walk through the steps required to dynamically add shipments and track numbers to a batch of orders in Magento.

Understanding the Requirement

You’ve mentioned that you need to update multiple orders with shipment information and track numbers in bulk. The process involves:

  1. Displaying a list of orders for an admin user.
  2. Allowing the admin user to enter track numbers for each order.
  3. Dynamically creating shipments and associating them with their respective orders using the entered track numbers.

Key Concepts to Consider

Before diving into the code, let’s clarify some key concepts:

  • Shipment: A shipment represents a package that is sent to a customer after an order has been placed. It can contain one or more items from the original order.
  • Track Number: This is a unique identifier assigned by the shipping carrier to monitor the movement of your package throughout the delivery process.

Step-by-Step Process

1. Create a New Magento Admin Module

To handle this functionality, we’ll need to create a new admin module in your Magento installation. This allows us to define custom routes and controllers for managing shipments.

Here’s a high-level overview of the files you’ll need:

  • app/code/Vendor/Module/etc/adminhtml/routes.xml: Define the admin route configuration.
  • app/code/Vendor/Module/Controller/Adminhtml/ShipmentController.php: Implement the controller logic to handle shipment creation and track number assignment.

2. Display the List of Orders

In your module, create a new view file (e.g., app/code/Vendor/Module/view/adminhtml/templates/shipment/list.phtml) that displays a list of orders using Magento’s standard order grid.

You can use the following code as a starting point:

<block class="Magento\Framework\View\Element\Template" template="Vendor_Module::list.phtml"/>

<referenceContainer name="content">
    <block class="Magento\Backend\Block\Widget\Grid" name="order_grid">
        <!-- Define your grid columns and actions here -->
    </block>
</referenceContainer>

3. Process the Form Submission

When an admin user submits the track numbers for each order, you’ll need to handle this form submission in your ShipmentController.php file.

Here’s a basic example of how to process the submitted data:

public function execute()
{
    $post = $this->getRequest()->getPostValue();

    foreach ($post['track_numbers'] as $orderId => $trackNumber) {
        // Create a new shipment for the order
        $shipment = $this->orderRepository->create();
        $shipment->setIncrementId('000000001');
        $shipment->setOrderId($orderId);
        $shipment->save();

        // Associate the track number with the shipment
        $track = $this->_trackingApi->getTrackingInfoByNumber($trackNumber, 'ups');
        if ($track) {
            foreach ($track as $item) {
                $shipment->addTrack($item);
            }
            $shipment->save();
        }
    }

    // Redirect back to the order grid
    $this->_redirect('vendor_module/shipment/index');
}

4. Use a Known Carrier for Shipment

To ensure consistency in your shipment process, you can use a known carrier like UPS or DHL. In Magento, carriers are defined in app/code/Magento/Shipping/etc/config.xml. You can create a new carrier configuration if required.

Best Practices and Pitfalls to Avoid

  • Always validate user input to prevent security vulnerabilities and data corruption.
  • Ensure you have proper error handling mechanisms in place for failed shipments or track number assignments.
  • Monitor your database performance, as creating many shipments simultaneously may impact the system’s response time.

Conclusion

Adding shipments and track numbers dynamically to Magento orders requires a combination of admin module development, form processing, and order management knowledge. By following this guide, you’ll be able to create an efficient batch process for updating multiple orders with shipment information and track numbers.

Remember that Magento provides extensive documentation on its architecture and APIs, which can serve as a valuable resource during your development journey.

To learn more about advanced Magento customizations and best practices, feel free to explore our Magento Development Services at Laravel Company. Our experienced team is ready to assist you with any complex Magento projects or integrations you may have in mind.

Extensions Admin Orders Customers

Our network