Hi,
I have a shipping module and I need to show the shipping method in the Order View in the admin.
I have created 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 getOrder() { $order = $this->order->loadByIncrementId($orderId); return $order; } }
and in view/adminhtml/templates/order/view
echo $order->getShippingAddress()->getShippingMethod();
or
echo $order->getShippingMethod();
and I get Undefined Variable order. What am I doing wrong?
Thanks,
Stan
Hi @stanhook ,
Ypu need to pass the increment_id of order to your function from phtml file for getting order information.
public function getOrder($increment_id) { $order = $this->order->loadByIncrementId($increment_id); return $order; }
And if you have order_id then try below mention way for fetching details
Hope this helps you!
Problem Solved! Click Kudos & Accept as Solution!
Hi @Nishu Jindal,
So I changed my function as you requested and placed this in the phtml file:
$orderDetails = $block->getOrder(); echo $orderDetails->getShippingAddress()->getShippingMethod();
and nothing happened. I also tried:
$shippingMethod = $order->getShippingDescription(); echo 'Shipping is ' . $shippingMethod();
What am I doing wrong.
Thanks,
Stan
Hi @Nishu Jindal ,
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 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();
Any other help would be great!
Thanks,
Stan
Hi @stanhook ,
1. Firstly add custom phtml in admin view using below link
2. Once custom phtml is called in admin view, then to get order I'd in your block inject registry and get the value like did in below link
https://www.rakeshjesadiya.com/add-custom-tab-in-admin-sales-order-view-magento-2/
Hope this helps you!
Problem Solved! Click Kudos & Accept as Solution!
Hi @Nishu Jindal,
The first link I already have completed, thanks. This isn't the trouble.
The second link I followed but I didn't have any success.
I have a post of the lates on Magento StackExchange:
Thanks for the help!
Stan
Hi @stanhook ,
Can you share your custom phtml and block code over here after the latest changes.
Thanks!