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.

0

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

| improve this answer | |
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;
}
| improve this answer | |
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);
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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