In my Custom Module Magento 2 I want to get Ordered product info by using order id using the Admin grid.
Currently, I get the order id but I want the details of the ordered product also how did I fetch that.
Any help is appreciated.
<?php
namespace Stackoverflow\OrderedProduct\Block;
class OrderItems extends \Magento\Framework\View\Element\Template
{
protected $_productloader;
protected $orderRepository;
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Magento\Catalog\Model\ProductFactory $_productloader,
\Magento\Sales\Api\OrderRepositoryInterface $orderRepository
) {
$this->_productloader = $_productloader;
$this->orderRepository = $orderRepository;
parent::__construct($context);
}
public function getLoadProduct($id)
{
return $this->_productloader->create()->load($id);
}
public functino getorderedProduct()
{
$orderId = '12';
$order = $this->orderRepository->get($orderId);
$items = $order->getAllVisibleItems();
foreach ($items as $item) {
$productId = $item->getProductId();
$product = $this->getLoadProduct($productId);
if($product->getSku()){
print_r($product->getData());
}
}
}
}
For admin grid i consider you have created grid using Ui_Compoonent. So consider the column definition as below
<column name="order_id" class="Stackoverflow\OrderedProduct\Ui\Component\Column\Product">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="filter" xsi:type="boolean">text</item>
<item name="filter" xsi:type="string">text</item>
<item name="label" xsi:type="string" translate="true">Product</item>
</item>
</argument>
</column>
Create Product.php under Stackoverflow\OrderedProduct\Ui\Component\Column and add below code
<?php
namespace Stackoverflow\OrderedProduct\Ui\Component\Column;
use Magento\Framework\View\Element\UiComponent\ContextInterface;
use Magento\Framework\View\Element\UiComponentFactory;
use Magento\Ui\Component\Listing\Columns\Column;
class Product extends Column {
protected $_productloader;
protected $orderRepository;
public function __construct(
ContextInterface $context,
UiComponentFactory $uiComponentFactory,
\Magento\Catalog\Model\ProductFactory $_productloader,
\Magento\Sales\Api\OrderRepositoryInterface $orderRepository
array $components = [],
array $data = []
) {
$this->_productloader = $_productloader;
$this->orderRepository = $orderRepository;
parent::__construct($context, $uiComponentFactory, $components, $data);
}
public function prepareDataSource(array $dataSource) {
if (isset($dataSource['data']['items'])) {
foreach ($dataSource['data']['items'] as &$item) {
if (isset($item['id_field_name']) && isset($item['order_id'])) {
$orderId = $item['order_id'];
$orderedProduct = $this->getorderedProduct($orderId);
if(!empty[$orderedProduct]){
$item = implode(",", $orderedProduct);
}
else
{
$item = 'N/A';
}
}
}
}
return $dataSource;
}
public function getLoadProduct($id)
{
return $this->_productloader->create()->load($id);
}
public functino getorderedProduct($orderId)
{
$productSku = []
$order = $this->orderRepository->get($orderId);
$items = $order->getAllVisibleItems();
foreach ($items as $item) {
$productId = $item->getProductId();
/*
Just to get sku you can use this code.
$productSku = $item->getProduct()->getSku();
*/
$product = $this->getLoadProduct($productId);
if($sku = $product->getSku()){
$productSku = $sku;
}
}
return $productSku;
}
}