1

I have modified the order grid layout by adding this code in:

vendor/magento/module-sales/view/adminhtml/ui_components/salesordergrid.xml

<column name="order_items" class="Magento\Sales\Ui\Component\Listing\Column\Items">
 <settings>
 <filter>textRange</filter>
 <label translate="true">Items</label>
 </settings>
 </column>

The "item" column is displaying now .

I created a class called

Magento\Sales\Ui\Component\Listing\Column\Items.php

See Below code:

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Magento\Sales\Ui\Component\Listing\Column;
use Magento\Framework\Escaper;
use Magento\Ui\Component\Listing\Columns\Column;
use Magento\Framework\View\Element\UiComponent\ContextInterface;
use Magento\Framework\View\Element\UiComponentFactory;
use Magento\Sales\Model\ResourceModel\Order\Status\CollectionFactory;
/**
 * Class Items
 */
class Items extends Column
{
 /**
 * @var Escaper
 */
 protected $escaper;
 /**
 * @param ContextInterface $context
 * @param UiComponentFactory $uiComponentFactory
 * @param Escaper $escaper
 * @param array $components
 * @param array $data
 */
 public function __construct(
 ContextInterface $context,
 UiComponentFactory $uiComponentFactory,
 Escaper $escaper,
 array $components = [],
 array $data = []
 ) {
 $this->escaper = $escaper;
 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) {
 $item[$this->getData('name')] = "test";/*nl2br($this->escaper->escapeHtml($item[$this->getData('name')]));*/
 }
 }
 return $dataSource;
 }
}

How do I get access to the product data for the current order to display it in the column ?

Right now I just have a place holder string : "test" outputted for every row.

$item[$this->getData('name')] = "test";

What class/method do I need to call to output the product data instead e.g.

$item[$this->getData('name')] = function that returns order product data .

Balwant Singh
1,2902 gold badges12 silver badges29 bronze badges
asked Sep 16, 2019 at 19:11

3 Answers 3

0

You can try this code in your PHP:

private $orderCollectionFactory;
public function __construct(
 ContextInterface $context,
 UiComponentFactory $uiComponentFactory,
 Escaper $escaper,
 \Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderCollectionFactory,
 array $components = [],
 array $data = []
) {
 $this->orderCollectionFactory = $orderCollectionFactory;
 $this->escaper = $escaper;
 parent::__construct($context, $uiComponentFactory, $components, $data);
}
public function prepareDataSource(array $dataSource)
{
 if (isset($dataSource['data']['items'])) {
 $orderIds = [];
 foreach ($dataSource['data']['items'] as $item) {
 $orderIds[] = $order->getId();
 }
 $orderCollection = $this
 ->orderCollectionFactory
 ->create()
 ->addFieldToFilter('entity_id', ['in' => $orderIds])
 ;
 foreach ($dataSource['data']['items'] as & $item) {
 $order = $orderCollection->getItemById($item['entity_id']);
 $data = '<ul>';
 foreach ($order->getAllVisibleItems() as $orderItem) {
 $data .= '<li>' . $orderItem->getName() . ' (SKU: ' . $orderItem->getSku() . ')</li>';
 }
 $data .= '</ul>';
 $item[$this->getData('name')] = $data;
 }
 }
 return $dataSource;
}

You can modify the HTML code to fit your display inside foreach ($order->getAllVisibleItems() as $orderItem) loop.

Note that to display a HTML data you need to modify your XML and add <bodyImpl>ui/grid/cells/html</bodyImpl>.

answered Sep 16, 2019 at 20:09
15
  • I'm not finished yet, I just accidentally clicked the submit button before finishing the code. Commented Sep 16, 2019 at 20:14
  • ok standing by , let me know when you are done . Thank you so much Commented Sep 16, 2019 at 20:15
  • done, you can try it. Commented Sep 16, 2019 at 20:32
  • Thank you sir , trying now Commented Sep 16, 2019 at 20:33
  • 1 exception(s): It throws the exception : Exception #0 (Exception): Notice: Undefined variable: orderCollectionFactory in /home/119528.cloudwaysapps.com/afdsqjqxmc/public_html/production/vendor/magento/module-sales/Ui/Component/Listing/Column/Items.php on line 46 @Rendy Eko Prastiyo Commented Sep 17, 2019 at 1:04
0

enter image description here

I have already developed custom magento extension for this functionality. You can download from here

https://drive.google.com/drive/u/3/folders/1p_YQiyMS2Wzcm4ilET_qPhkZ6LPSEBhn

answered Apr 14, 2021 at 7:53
0
<column name="order_items" class="Harriswebworks\OrderAttribute\Ui\Component\Listing\Column\Items">
 <argument name="data" xsi:type="array">
 <item name="config" xsi:type="array">
 <!-- <item name="filter" xsi:type="string">textRange</item> -->
 <item name="label" xsi:type="string" translate="true">Items</item>
 </item>
 </argument> 
 </column>
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Harriswebworks\OrderAttribute\Ui\Component\Listing\Column;
use Magento\Framework\Escaper;
use Magento\Ui\Component\Listing\Columns\Column;
use Magento\Framework\View\Element\UiComponent\ContextInterface;
use Magento\Framework\View\Element\UiComponentFactory;
use Magento\Sales\Model\ResourceModel\Order\Status\CollectionFactory;
/**
 * Class Items
 */
class Items extends Column
{
 /**
 * @var Escaper
 */
 protected $escaper;
 /**
 * @param ContextInterface $context
 * @param UiComponentFactory $uiComponentFactory
 * @param Escaper $escaper
 * @param array $components
 * @param array $data
 */
 public function __construct(
 ContextInterface $context,
 UiComponentFactory $uiComponentFactory,
 Escaper $escaper,
 \Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderCollectionFactory,
 array $components = [],
 array $data = []
 ) {
 $this->escaper = $escaper;
 $this->orderCollectionFactory = $orderCollectionFactory;
 parent::__construct($context, $uiComponentFactory, $components, $data);
 }
 /**
 * Prepare Data Source
 *
 * @param array $dataSource
 * @return array
 */
 public function prepareDataSource(array $dataSource)
 {
 if (isset($dataSource['data']['items'])) {
 $orderIds = [];
 foreach ($dataSource['data']['items'] as & $item) { 
 $orderIds[] = $item['entity_id']; 
 }
 $orderCollection = $this->orderCollectionFactory->create()->addFieldToFilter('entity_id', ['in' => $orderIds]);
 foreach ($dataSource['data']['items'] as & $item) {
 $order = $orderCollection->getItemById($item['entity_id']);
 $data = '';
 foreach ($order->getAllVisibleItems() as $orderItem) {
 //$data .= $orderItem->getName() . ' (SKU: ' . $orderItem->getSku() . ")\n";
 $data .= $orderItem->getSku() . "\n";
 }
 
 $item[$this->getData('name')] = $data;
 } 
 }
 return $dataSource;
 }
}
answered Jan 24, 2022 at 20:57

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.