1

I have created a shipment by adding Tacking info. I am getting the shipment email with empty Tacking info.

Next time if i click on "send Tracking Information. Then I will get the tracking info in shipment email.

My Magento version is 2.3.5

here is the screenshot of shipment email enter image description here

I need to get this info during shipment creation.

Hopefully its because i used this event to send shipment email automatically whenever the shipment is created.

Vendor/Module/etc/events.xml

<event name="sales_order_shipment_save_after">
    <observer name="sales_order_shipment_after"
              instance="Vendor\Module\Observer\SalesOrderShipmentAfter" />
</event>

Vendor/Module/Observer/SalesOrderShipmentAfter.php

use Magento\Framework\Event\ObserverInterface;
class SalesOrderShipmentAfter implements ObserverInterface
{
    protected $shipmentNotifier;
    protected $_logger;
    public function __construct(
    \Psr\Log\LoggerInterface $logger,       
    \Magento\Shipping\Model\ShipmentNotifier $shipmentNotifier
) {
    $this->_logger = $logger;
    $this->shipmentNotifier = $shipmentNotifier;
}
public function execute(\Magento\Framework\Event\Observer $observer)
{
    try{
        $this->_logger->info('inside shipment observer');
        $shipment = $observer->getEvent()->getShipment();
        $this->shipmentNotifier->notify($shipment);
        //$shipment->save();
    }catch (\Exception $e) {
        $this->_logger->info('--shipment observer--'.$e->getMessage());
    } 
  }
}

Can anyone please help me on this. Thanks

2
  • Is happen when you trying to sent from admin?
    – Amit Bera
    Commented Jun 29, 2020 at 6:28
  • @AmitBera, yes, whenever we create shipment from admin, by selecting send an email copy checkbox
    – Manjunath
    Commented Jun 29, 2020 at 6:31

1 Answer 1

0

Step 1: First create event.xml

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

<?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) {
        }
    }
}


?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.