1

I have a module that places some shipping information into the order view of the admin. I am trying to display the current shipping method in this section.

I have this in view/adminhtml/templates/order/view/custom_fields.phtml:

$orderId = $this->getRequest()->getParam('order_id');
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$order = $objectManager->create('Magento\Sales\Api\Data\OrderInterface')->load($orderId);

$shippingMethod = $order->getShippingDescription();

echo 'Shipping is ' . $order->getShippingMethod();

This works fine but I know it isn't the proper way to use Object Manager. Can someone help me use this properly?

Thanks, Stan

2
  • Could you please show layout xml where you have considered this phtml in order view block ? Sep 24, 2020 at 17:58
  • @AbdulPathan Sure. Why? Like I said this shows me the data I need. I just know it isn't the way we are supposed to use object manager. <page xmlns:xsi="w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd"> <body> <block class="Magento\Backend\Block\Template" name="order_custom_fields" template="Vendor_Module::order/view/custom_fields.phtml" /> </body> </page>
    – stanhook
    Sep 24, 2020 at 18:00

1 Answer 1

2

Please follow the below steps to get the shipping method and its details in phtml file.

app/code/Test/RoomDelivery/view/adminhtml/layout/sales_order_view.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="sales_order_edit">
            <block class="Test\RoomDelivery\Block\Adminhtml\Order\CustomFields" name="order_custom_fields" template="Test_RoomDelivery::order/view/custom_fields.phtml" />
        </referenceBlock>
    </body>
</page>

app/code/Test/RoomDelivery/Block/Adminhtml/Order/CustomFields.php

declare(strict_types=1);

namespace Test\RoomDelivery\Block\Adminhtml\Order;

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

/**
 * Class CustomFields
 *
 * @category Block/Order
 * @package  Test\RoomDelivery\Block\Adminhtml\Order
 */
class CustomFields extends Template
{
   
    /**
     * Core registry
     *
     * @var Registry
     */
    protected $coreRegistry = null;

    /**
     * CustomFieldsRepositoryInterface
     *
     * @var CustomFieldsRepositoryInterface
     */
    protected $customFieldsRepository;

    /**
     * CustomFields constructor.
     *
     * @param Context                         $context                Context
     * @param Registry                        $registry               Registry
     * @param CustomFieldsRepositoryInterface $customFieldsRepository CustomFieldsRepositoryInterface
     * @param array                           $data                   Data
     */
    public function __construct(
        Context $context,
        Registry $registry,
        array $data = []
    ) {
        $this->coreRegistry = $registry;
        parent::__construct($context, $data);
    }

    /**
     * Get current order
     *
     * @return Order
     */
    public function getOrder() : Order
    {
        return $this->coreRegistry->registry('current_order');
    } 
}

app/code/Test/RoomDelivery/view/adminhtml/templates/order/view/custom_fields.phtml

$order = $block->getOrder();
$orderDetails = $order->getId();
echo 'details ' . $orderDetails . '<br />';
echo $order->getShippingDescription();

Please let me know if you are facing any issue.

I hope this will help you.

1
  • That was it, thanks for the answer and giving back to the community!
    – stanhook
    Sep 28, 2020 at 19:07

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.