2

I am trying to get the shipping method using Object Manager

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$order = $objectManager->create('Magento\Sales\Api\Data\OrderInterface')->load($orderid);

by doing var_dump on $order->getShippingAddress()->getData() i get no such thing as shipping method, from where i can get shipping method? I am new to magento.

after referring to this link

Magento2: How to get shipping method in order using observer `sales_order_save_after`?

I don't know how to use sales_order_save_after if that's the only solution out there.

1
  • It is not necessary to use sales_order_save_after event to print the shipping method. Commented Jan 3, 2019 at 13:48

3 Answers 3

1

You can try below code to get shipping method.

$shippingMethod = $order->getShippingDescription();
$order->getShippingMethod();

It's getting the shipping method. Still facing any issue let me know.

3
  • it prints data correctly but it has extra strings also which is useless, string(20) "Free Ground Shipping" string(20), so i am assuming getShippingDescription() gives arrray, i can access it using its array index key also which is table's column name how to do i kind that index key
    – suman
    Commented Jan 3, 2019 at 13:17
  • You can getting shipping method code using $order->getShippingMethod(); this. Commented Jan 3, 2019 at 13:45
  • thanks $shippingMethod = $order->getShippingDescription(); works for me
    – suman
    Commented Jan 3, 2019 at 18:38
1

You are doing var_dump on $order->getShippingAddress()->getData(). Instead of that, do var_dump on $order->getData() and you will be able to see shipping_method.

You can get the shipping method by

$order->getShippingMethod();

or

$order->getShippingAddress()->getShippingMethod();
2
  • $order->getShippingMethod(); gives "freeshipping_freeshipping" and but i want "Free Ground Shipping" so $order->getShippingDescription(); works for me what's the difference between these two
    – suman
    Commented Jan 3, 2019 at 18:37
  • Don't do this it will hang you browser even pc [var_dump($order->getData())]
    – matinict
    Commented Feb 28, 2021 at 11:59
1

You will have to create an observer

events.xml

<?xml version="1.0"?>
<event name="sales_order_save_before">
    <observer name="order_save_after" instance="Vendor\ModuleName\Observer\OrderSaveAfter" />
</event>

OrderSaveAfter.php

<?php
namespace Vendor\ModuleName\Observer;

use Magento\Framework\Event\ObserverInterface;

class OrderSaveAfter implements ObserverInterface {

    public function execute(\Magento\Framework\Event\Observer $observer) {
        $order = $observer->getEvent()->getOrder();
        $order->getShippingMethod();
    }
}

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.