Skip to content

Magento 2 get tracking infomation is empty

i am using this for sending email

 <event name="sales_order_shipment_save_after">
        <observer name="sales_order_shipment_after"
                  instance="BananCheckoutObserverSalesOrderShipmentAfter" />
    </event>
      <event name="email_shipment_set_template_vars_before">
        <observer name="Additional_Variable_For_ShipmetEmail"
                  instance="BananCheckoutObserverAdditionalVariable"/>
    </event>

namespace BananCheckoutObserver;

use Exception;
use MagentoFrameworkEventObserverInterface;
use MagentoFrameworkEventObserver;
use MagentoShippingModelCarrierFactory;
use BananMarketplaceHelperData as MarketPlaceHelper;
use BananSellerHelperData as SellerHelper;
use PsrLogLoggerInterface;
use MagentoFrameworkAppRequestHttp;

//class for set additonal variable
class AdditionalVariable implements ObserverInterface
{
    protected CarrierFactory $carrierFactory;
    protected MarketPlaceHelper $marketPlaceHelper;
    protected SellerHelper $sellerHelper;
    protected LoggerInterface $logger;
    protected $request;

    public function __construct(
        CarrierFactory $carrierFactory,
        MarketPlaceHelper $marketPlaceHelper,
        SellerHelper $sellerHelper,
        LoggerInterface $logger,
        Http $request
    ) {
        $this->carrierFactory = $carrierFactory;
        $this->marketPlaceHelper = $marketPlaceHelper;
        $this->sellerHelper = $sellerHelper;
        $this->logger = $logger;
        $this->request = $request;
    }
    public function execute(Observer $observer)
    {
        try {
            $transport = $observer->getTransport();
            if (!is_array($transport)) {
                return;
            }
              $shipment = $transport['shipment'] ?? null;
            $order = $transport['order'] ?? null;
         
            $trackingNumber = $carrierTitle = "";
            $items = $shipment->getAllItems();
            foreach ($items as $item) {
                $productId =  $item->getProductId();
                $marketplaceProduct = $this->marketPlaceHelper->getSellerProductDataByProductId($productId);
                $sellerId = 0;
                foreach ($marketplaceProduct as $value) {
                    $sellerId = $value['seller_id'] ?? '';
                }
                $sellerInfo = $this->marketPlaceHelper->getSellerInfo($sellerId);
                $sellerEmailID = $sellerInfo['email'] ?? '';
            }
            $transport = $observer->getData('transportObject');
            $transport->setData('seller_email', $sellerEmailID);
            $trackCollection = $order->getTracksCollection($shipment->getId());
            
            foreach ($trackCollection as $track) {
               
                $transport->setData('delivery_service_provider', $track->getTitle());
                $transport->setData('tracking_number', $track->getTrackNumber());
            }
            $trackingNumber='';
            if (!empty($shipment->getTracks())) {
                foreach ($shipment->getTracks() as $track) {
                    $trackingNumber = $track->getTrackNumber();
                }
            }
        } catch (Exception $e) {
            $this->logger->error($e->getMessage());
        }

        return;
    }
}

in this tracking details is empty