I have a custom shipping module that passes some information into the Sales Order and the email. The problem is that it shows the data even when the shipping method isn't used. How can I get the shipping method so I can use it to only display the information if that shipping method is chosen?

This adds my data into the Sales Order in the Admin - view/adminhtml/templates/order/view

$formFields = $block->getOrderFormFields();
<?php if($shippingMethod == 'SHIPPING METHOD CODE'): ?>
    <section class="admin__page-section">
        <div class="admin__page-section-title">
            <span class="title"><?php /* @escapeNotVerified */ echo __('Shipping Option') ?></span>
        <div class="admin__page-section-content">
            <div class="order-information">
                <div class="box">
                    <strong class="box-title"><span><?php /* @escapeNotVerified */ echo __('Building Address') ?></span></strong>
                    <div class="box-content">
                        <?php echo $this->escapeHtml($formFields->getCheckoutBuildingAddress()); ?>
                <div class="box">
                    <strong class="box-title"><span><?php /* @escapeNotVerified */ echo __('Floor Number') ?></span></strong>
                    <div class="box-content">
                        <?php echo $this->escapeHtml($formFields->getCheckoutFloorNumber()); ?>
                <div class="box">
                    <strong class="box-title"><span><?php /* @escapeNotVerified */ echo __('Room Number') ?></span></strong>
                    <div class="box-content">
                        <?php echo $this->escapeHtml($formFields->getCheckoutRoomNumber()); ?>
<?php endif; ?>

How can I get the shipping code from the order so this only displays when the method is chosen?

UPDATE I added a block class in Block/Order:

namespace Vendor\Module\Block\Order;

use Magento\Framework\View\Element\Template;
use Magento\Framework\View\Element\Template\Context;
use Magento\Framework\Registry;
use Magento\Sales\Model\Order;

class OrderItems extends \Magento\Framework\View\Element\Template

    protected $order;

    public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        \Magento\Sales\Model\Order $order,
        array $data = []
    ) {
        $this->order = $order;
        parent::__construct($context, $data);

     * Get current order
     * @return Order

    public function _prepareLayout()
        return parent::_prepareLayout();

    public function getOrderItems()
        $order = $this->order->loadByIncrementId($orderId);

        return $order->getItems();


And then tried this code in view/adminhtml/templates/order/view"

$order = $block->getOrderItems();
$orderItems = $order->getAllItems();
$code = $orderItems->getShippingMethod();

echo 'order' . $code;

Nothing happens.

UPDATE 9.16.2020 This works in my phtml file with the exception the Object Manager shouldn't be used this way and it doesn't get the current order ID when the page is displayed.

$orderId = 1;
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); 
$order = $objectManager->create('\Magento\Sales\Model\OrderRepository')->get($orderId);
// Fetch whole billing information
// Fetch specific billing information
echo $order->getBillingAddress()->getCity();
echo $order->getBillingAddress()->getRegionId();
echo $order->getBillingAddress()->getCountryId();
// Fetch whole shipping information
// Fetch specific shipping information
echo $order->getShippingAddress()->getCity();
echo $order->getShippingAddress()->getRegionId();
echo $order->getShippingAddress()->getCountryId();
echo $order->getShippingMethod();

Thanks, Stan


2 Answers 2


In admin side order object is there.

You need to add this line to call it.



  • Thanks. I tried ` $shipping = $_order->getShippingMethod(); echo 'shipping ' . $shipping; ` And nothing shows. I guess I am using it wrong. What am I missing?
    – stanhook
    Sep 11, 2020 at 13:54
  • In your code $_order object is there? Please get all details via object in $_order and then call it. Sep 11, 2020 at 14:08
  • Via this code you can get order details. $_order = $this->_objectManager->create('Magento\Sales\Model\Order')->load($orderId); $orderItems = $_order->getAllItems(); Sep 11, 2020 at 14:10
  • "In your code $_order object is there?" - Yes. But I guess that is where I get lost some. "Please get all details via object in $_order and then call it." I am not sure what you mean. Can you explain it some more please?
    – stanhook
    Sep 11, 2020 at 15:15
  • I tried this: $_order = $this->_objectManager->create('Magento\Sales\Model\Order')->load($orderId); $orderItems = $_order->getAllItems(); echo 'order' . $_order; print_r($_order); And just got an "Undefined variable: _objectManager" when I tried to view the order in the admin. I also tried using $_objectManager and got the same error.
    – stanhook
    Sep 11, 2020 at 15:36

You can get the shipping method by




Or May be you need to create new function in the block and send order object instead of order item object forEx:

  public function getOrder()
        $order = $this->order->loadByIncrementId($orderId);

        return $order;


In HTML/view file, you will get shipping method using the below code.


I hope this will help you.

Please let me know if this will not work. If this will work then please accept this as ANSWER. Thanks

  • Hi, I used the Block I have in my edit above (with your change for getOrder()) and tried the following in my phtml file: echo $order->getShippingMethod; and received the error: "Undefined variable: order" in my phtml file. What am I doing wrong? Thanks, Stan
    – stanhook
    Sep 15, 2020 at 17:51
  • Please send me the code.
    – Nits
    Sep 16, 2020 at 6:57
  • I sent an email and also made an update above in my original answer - 9.16.2020
    – stanhook
    Sep 16, 2020 at 19:42

Your Answer

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

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