0

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.

  • It is not necessary to use sales_order_save_after event to print the shipping method. – Manashvi Birla Jan 3 at 13:48
0

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.

  • 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 Jan 3 at 13:17
  • You can getting shipping method code using $order->getShippingMethod(); this. – Dharmendra Jadav Jan 3 at 13:45
  • thanks $shippingMethod = $order->getShippingDescription(); works for me – suman Jan 3 at 18:38
0

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();
  • $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 Jan 3 at 18:37
0

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 acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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