How get order items using entity_id
-
21-02-2021 - |
Question
This File responsible for setting Grand Total in Order Grid. I test it.
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Sales\Ui\Component\Listing\Column;
use Magento\Framework\View\Element\UiComponent\ContextInterface;
use Magento\Framework\View\Element\UiComponentFactory;
use Magento\Ui\Component\Listing\Columns\Column;
use Magento\Framework\Pricing\PriceCurrencyInterface;
/**
* Class Price
*/
class Price extends Column
{
/**
* @var PriceCurrencyInterface
*/
protected $priceFormatter;
/**
* Constructor
*
* @param ContextInterface $context
* @param UiComponentFactory $uiComponentFactory
* @param PriceCurrencyInterface $priceFormatter
* @param array $components
* @param array $data
*/
public function __construct(
ContextInterface $context,
UiComponentFactory $uiComponentFactory,
PriceCurrencyInterface $priceFormatter,
array $components = [],
array $data = []
) {
$this->priceFormatter = $priceFormatter;
parent::__construct($context, $uiComponentFactory, $components, $data);
}
/**
* Prepare Data Source
*
* @param array $dataSource
* @return array
*/
public function prepareDataSource(array $dataSource)
{
if (isset($dataSource['data']['items'])) {
foreach ($dataSource['data']['items'] as & $item) {
$currencyCode = isset($item['base_currency_code']) ? $item['base_currency_code'] : null;
$item[$this->getData('name')] = $this->priceFormatter->format(
$item[$this->getData('name')],
false,
null,
null,
$currencyCode
);
}
}
return $dataSource;
}
}
$item array return all orders i print one order print_r($item)
die('');
Array ( [id_field_name] => entity_id [entity_id] => 53 [status] => pending [store_id] => Main Website Main Website Store Default Store View [store_name] => Main Website Main Website Store [customer_id] => 1 [base_grand_total] => 2623.9800 [base_total_paid] => [grand_total] => 2623.9800 [total_paid] => [increment_id] => 000000130 [base_currency_code] => USD [order_currency_code] => USD [shipping_name] => Veronica Costello [billing_name] => Veronica Costello [created_at] => 2019-02-25 19:46:38 [updated_at] => 2019-02-25 14:46:38 [billing_address] => 6146 Honey Bluff Parkway Calder Michigan 49628-7978 [shipping_address] => 6146 Honey Bluff Parkway Calder Michigan 49628-7978 [shipping_information] => Best Way - Table Rate [customer_email] => roni_cost@example.com [customer_group] => 1 [subtotal] => 3030.0000 [shipping_and_handling] => 0.0000 [customer_name] => Veronica Costello [payment_method] => checkmo [total_refunded] =>
I want to get order Items from sales_order_item table when I give a specific id like => $item['entity_id']
then it return all the order items of specific id $item['entity_id']
.
How can do this? kindly guide me little bit.
Solution
Try this,
Add the below code in your controller
<?php
namespace Vendor\ModuleName\Controller\Orders;
class ReadOrders extends \Magento\Framework\App\Action\Action
{
protected $resultPageFactory;
protected $itemFactory;
public function __construct(
\Magento\Sales\Model\Order\ItemFactory $itemFactory,
\Magento\Framework\App\Action\Context $context,
\Magento\Framework\View\Result\PageFactory $resultPageFactory
) {
$this->itemFactory = $itemFactory;
$this->resultPageFactory = $resultPageFactory;
parent::__construct($context);
}
public function execute()
{
$entity_id = 1; // give your entity_id here to get the details of items
$order = $this->itemFactory->create()->getCollection()->addFieldToFilter('order_id', $entity_id);
foreach ($order as $items) {
echo 'Order Id :'.$itemId = $items->getItemId() . '<br>';
echo 'Status :'.$items->getStatus(). '<br>';
echo 'Product Name :'.$items->getName(). '<br>';
}
}}
Hope this helps :)