0

I have created a custom module for order tracking.

Now when the order is completed the shipment id is generated.

So, How to get the shipment id?

I have my block like this :

<?php
/**
 * Copyright © 2016 Codazon. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Codazon\AjaxCartPro\Block;
class Trackorder extends \Magento\Framework\View\Element\Template
{

    protected $orderRepository;
    protected $_coreRegistry;

    public function __construct(      
        \Magento\Sales\Api\OrderRepositoryInterface $orderRepository,
    \Magento\Framework\Registry $coreRegistry,
    \Magento\Framework\View\Element\Template\Context $context,
        array $data = []      
    )
    {    

         $this->_coreRegistry = $coreRegistry;
         $this->orderRepository = $orderRepository;   
         parent::__construct($context, $data);   
    }

    public function getOrderStatus()
    {

        $orderId = $this->_coreRegistry->registry('order_id');
        $order = $this->orderRepository->get($orderId);
        $state = $order->getState(); // Print Order State(Complete, Processing, ....)
        return $state;
    }   

    public function getShipmentID()
    {
        $orderId = $this->_coreRegistry->registry('order_id');
        $order = $this->orderRepository->get($orderId);
        $shipment = $order->getShipmentsCollection();
        return $shipment;
    }
}

But getShipmentID() this function does not work for me. How to resolve this problem?

How to get shipment id If order is completed.

4 Answers 4

2

STEP-BY-STEP PROCESS TO GET SHIPMENT TRACKING INFORMATION PROGRAMMATICALLY IN MAGENTO 2 Please follow the below steps to learn how to get shipment tracking information in Magento 2.

STEP – 1 First of all, create an event.xml file in the app\code\Vendor\Extension\etc\ folder and paste 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 Next, open the Shipment.php file from the app\code\Vendor\Extension\Observer\ folder and paste the following code.

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

And it’s done!

0

The class is in Magento\Sales\Model\Order. Please check the Shippment collection. Hope it will helpful for you

0

Your getShipmentID() method need some modification.

Try following way:


public function getShipmentID()
{
    $orderId = $this->_coreRegistry->registry('order_id');
    $order = $this->orderRepository->get($orderId);
    $shipmentCollection = $order->getShipmentsCollection();
    $shipmentId = 0;
    foreach ($shipmentCollection as $shipment) {
        $shipmentId = $shipment->getId();
    }
    return $shipmentId;
}

If order has multiple shipment then try following way:


public function getShipmentID()
{
    $orderId = $this->_coreRegistry->registry('order_id');
    $order = $this->orderRepository->get($orderId);
    $shipmentCollection = $order->getShipmentsCollection();
    $shipmentId = [];
    foreach ($shipmentCollection as $shipment) {
        $shipmentId[] = $shipment->getId();
    }
    return $shipmentId;
}
1
  • hi @Sohel, I have used event "sales_order_shipment_save_after" and in the observer tried this $shipment = $observer->getEvent()->getShipment(); $shipmentId = $shipment->getId();, shipmentid not returning Commented Nov 4, 2020 at 6:37
0

if anyone needs to use in objmanager try this.

    $order_id = 140;
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); 
    $shipmentCollection = $objectManager->create('\Magento\Sales\Api\OrderRepositoryInterface');
    $orderRepository = $shipmentCollection->get($order_id);
    $shipmenttotal = $orderRepository->getShipmentsCollection();
    foreach ($shipmenttotal as $shipment) {
        $shipmentId = $shipment->getId();
    }
    print_r($shipmentId);

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.