How To

How to Get Tracking Information From Shipment in Magento 2

Hello Magento Folks? ,

How are you doing? Today I am here with yet another useful topic How to Get Tracking Information From Shipment in Magento 2. Look at our previously released blog, How to Add Dynamic Link to Footer in Magento 2 if you skipped out.

Introduction

Customer experience has become the central point for the E-commerce industry. The customers’ journey to your store starts by examining the product to purchasing it and then receiving the product. After the order placement, it is very essential to stay connected with the customer by providing information about their order like shipment status of the product, delivery date, and the reason for the delay. This will help to build the trust of the customers and result in improved user experience.

Magento 2 provides the feature to notify tracking information to the customers as well as the store owner. Let’s get rolling!

Steps to Get Tracking Information From Shipment in Magento 2

By the following code, you will be able to Get Tracking Information From Shipment in Magento 2. It can be done by event-observer as shown below:

Step 1: First create event.xml

Path- app\code\Vendor\Extension\etc\events.xml

Now enter the following code.

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="sales_order_shipment_save_after">
          <observer name="track_shipment" instance="Vendor\Extension\Observer\Shipment" />
     </event>
</config>

Step 2: Then enter the following code in Observer.

Path – app\code\Vendor\Extension\Observer\Shipment.php

<?php
namespace Vendor\Extension\Observer;
use Magento\Framework\Event\ObserverInterface;
class Shipment implements ObserverInterface
{
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        try {
            $shipment = $observer->getEvent()->getShipment();
            $tracksCollection = $shipment->getTracksCollection();

            foreach ($tracksCollection->getItems() as $track) {
                $track_number = $track->getTrackNumber();
                $carrier_name = $track->getTitle();
            }
        } catch (\Exception $e) {
        }
    }
}


?>

You are done!

Closing Words:

Hope, now you know How to Get Tracking Information From Shipment in Magento 2. We recommend you take advantage of Magento 2 Order Tracking Extension to enhance your store performance and provide greater customer services. In case any complications let me know in the comment area and I will get back to you. If you find it worthful, share it with your Magento pals. Stay safe and stay tuned so that you do not miss any important functionality for your store!

Happy Coding ?

Click to rate this post!
[Total: 7 Average: 4.7]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate ?️ Certified Magento Developer?‍?. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.?

View Comments

Recent Posts

Magento 2: How to Save Custom Field Value to quote_address for Multi-Shipping Orders

Hello Magento Friends, In Magento 2, the checkout process allows customers to choose multiple shipping…

5 hours ago

Best Beginners Guide to Shopify Balance Account

If you are a Shopify admin, using a Shopify Balance Account for your business revenue…

5 hours ago

8 Best Social Login Apps for Shopify Store in 2024

Running an eCommerce business can be incredibly demanding, leaving entrepreneurs little time to focus on…

5 hours ago

Generating Thumbnails with Spatie Media Library in Laravel 11: A Step-by-Step Guide

Generating image thumbnails is a common requirement in web applications, especially when handling media-heavy content.…

1 day ago

Enhancing Web Application Security with Laravel’s Built-In Features

In today’s digital landscape, web application security is paramount. As a powerful PHP framework, Laravel…

2 days ago

Magento 2 Extensions Digest October 2024 (New Release & Updates)

October was an exciting month for MageComp! From significant updates across our Magento 2 extension…

2 days ago