cancel
Showing results for 
Search instead for 
Did you mean: 

Getting Ordering info in Admin Sales Order

Getting Ordering info in Admin Sales Order

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

7 REPLIES 7

Re: Getting Ordering info in Admin Sales Order

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 

https://www.codextblog.com/code-snippet/get-order-information-from-order-id-in-magento-2/#:~:text=fe...

 

Hope this helps you!

Problem Solved! Click Kudos & Accept as Solution!

 

Re: Getting Ordering info in Admin Sales Order

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

Re: Getting Ordering info in Admin Sales Order

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

Re: Getting Ordering info in Admin Sales Order

Hi @stanhook ,

1. Firstly add custom phtml in admin view using below link

https://community.magento.com/t5/Magento-2-x-Admin-Configuration/Custom-Block-in-admin-Order-view-of...

 

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!

Re: Getting Ordering info in Admin Sales Order

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:

 

https://magento.stackexchange.com/questions/322511/magento-2-get-shipping-code-or-method-in-sales-or...

 

Thanks for the  help!

Stan

Re: Getting Ordering info in Admin Sales Order

Hi @stanhook ,

 

Can you share your custom phtml and block code over here after the latest changes.

 

Thanks!

Re: Getting Ordering info in Admin Sales Order