Hi,
I am trying to get some order information for a shipping module to display in the admin. 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(); $shippingMethod = $order->getShippingmethod(); echo 'Shipping is ' . $order->getShippingMethod();
This gets the information just fine. But I know that I shouldn't use Object Manager this way. I tried something in Block/Order/Info.php and something in my di.xml file but that didn't work. What do I need to do so this works the right way?
Thanks!
Solved! Go to Solution.
Hi,
I finally got a solution and it is here:
https://magento.stackexchange.com/questions/322994/magento-2-using-object-manager
Thanks for all the help!
Hi @stanhook
You need to create customer controller, block and layout.xml for specific page where you are using this phtml file.
For example:
namespace Vendor\Module\Controller\Category;
class View extends \Vendor\Module
\Controller\Category
{
/**
* @var \Magento\Framework\Controller\Result\ForwardFactory
*/
protected $_coreRegistry = null;
/**
* @var \Magento\Framework\View\Result\PageFactory
*/
protected $resultPageFactory ;
/**
* @param \Magento\Backend\App\Action\Context $context
* @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
*/
public function __construct( \Magento\Framework\App\Action\Context $context,
\Magento\Framework\View\Result\PageFactory $resultPageFactory,
\Magento\Framework\Registry $registry
) {
$this->resultPageFactory = $resultPageFactory;
$this->_coreRegistry = $registry;
parent::__construct($context);
}
/**
* Product list page
*
* @return \Magento\Backend\Model\View\Result\Page
*/
public function execute()
{
$resultPage = $this->resultPageFactory ->create();
$blockInstance = $resultPage->getLayout()->getBlock('category.index');
}
}
layout in frontend
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="content">
<block class="Vendor\Module\Block\Faq" name="category.index" template="Addon_Faq::view.phtml"/>
</referenceContainer>
</body>
</page>
your xml name should be controller_modle_view.xml.
and create custom block by name
Vendor\Module\Block\Faq
Where you can create your custom function which can you in phtml file directly.
Hope it may help you!
Problem Solved? Please click on 'Kudos' & Accept as Solution!
@stanhook you can create a Block and try the below code
namespace EmizenTech\Module\Block; use Magento\Catalog\Api\ProductRepositoryInterface; class Order extends \Magento\Framework\View\Element\Template { protected $order; public function __construct( \Magento\Sales\Api\Data\OrderInterface $order, ) { $this->order = $order; } public function getOrder() { $orderId = $this->getRequest()->getParam('order_id'); $order = $this->order->load($orderId); } }
inside the phtml file, you can just call below code
$order = $block->getOrder(); $shippingMethod = $order->getShippingDescription(); $shippingMethod = $order->getShippingmethod(); echo 'Shipping is ' . $order->getShippingMethod();
I hope it helps!
Hi @amitsamsukha,
I tried the code as you have it:
use Magento\Catalog\Api\ProductRepositoryInterface; /** * Invoice view comments form * * @api * @author Magento Core Team <core@magentocommerce.com> * @since 100.0.2 */ class Info extends \Magento\Framework\View\Element\Template { protected $order; public function __construct( \Magento\Sales\Api\Data\OrderInterface $order ) { $this->order = $order; } /** * Retrieve current order model instance * * @return \Magento\Sales\Model\Order */ public function getOrder() { $orderId = $this->getRequest()->getParam('order_id'); $order = $this->order->load($orderId); } }
in my phtml:
$order = $block->getOrder(); echo 'order id ' . $order . '<br />'; $shippingMethod = $order->getShippingmethod(); echo 'Shipping is ' . $shippingMethod;
and received this error:
Uncaught Error: Call to a member function getShippingmethod() on null
I can't even get the order id with
$order = $block->getOrder();
echo 'order id ' . $order . '<br />';
Nothing shows.
Thought's?
kindly check the order is you are using is a parameter not into post &
its it should be entity id, not increment id.
Hi @amitsamsukha,
So I tried it with just entering a number:
$orderId = $this->getRequest()->getParam('268'); $order = $this->order->load($orderId);
and just in case I tried this:
$orderId = $this->getRequest()->getParam(268); $order = $this->order->load($orderId);
And this
$orderId = '268'; $order = $this->order->load($orderId);
This is in the URL:
sales/order/view/order_id/268/key/
And still nothing happens.
Just to note, that if I do this:
public function getTheInfo() { $info = 'hello'; return $info; }
and this in my phtml file:
echo 'order is '. $block->getTheInfo();
nothing other the the "order is" text appears, "hello" is not there. So I must be doing something wrong. Just to note, that on this page there is other information that is showing in the admin so I know that page is working and being called. I just can't seem to get anything else to work.
Thanks.
Stan
Hi,
I finally got a solution and it is here:
https://magento.stackexchange.com/questions/322994/magento-2-using-object-manager
Thanks for all the help!