Skip to content

Magento 2 – Get Shipping Code in Sales Order view in Admin

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

<?php
$formFields = $block->getOrderFormFields();
$shippingMethod = SOME CODE TO GET SHIPPING METHOD CODE;
?>
<?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>
        <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>
                </div>
                <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>
                </div>
                <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()); ?>
                    </div>
                </div>
            </div>
        </div>
    </section>
<?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 VendorModuleBlockOrder;

use MagentoFrameworkViewElementTemplate;
use MagentoFrameworkViewElementTemplateContext;
use MagentoFrameworkRegistry;
use MagentoSalesModelOrder;

class OrderItems extends MagentoFrameworkViewElementTemplate
{

    protected $order;

    public function __construct(
        MagentoBackendBlockTemplateContext $context,
        MagentoSalesModelOrder $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 = MagentoFrameworkAppObjectManager::getInstance(); 
$order = $objectManager->create('MagentoSalesModelOrderRepository')->get($orderId);
  
// Fetch whole billing information
print_r($order->getBillingAddress()->getData());
   
// Fetch specific billing information
echo $order->getBillingAddress()->getCity();
echo $order->getBillingAddress()->getRegionId();
echo $order->getBillingAddress()->getCountryId();
   
// Fetch whole shipping information
print_r($order->getShippingAddress()->getData());
   
// Fetch specific shipping information
echo $order->getShippingAddress()->getCity();
echo $order->getShippingAddress()->getRegionId();
echo $order->getShippingAddress()->getCountryId();
echo $order->getShippingMethod();

Thanks,
Stan