2

everyone, I want to display product SKU in admin grid I have the product name .

code for a display product name is below

ui_componeny/index.xml

<column name="entity_id" 
 class="Magneto\SampleRequest\Ui\Component\Listing\Column\Products"> 
        <argument name="data" xsi:type="array"> 
        <item name="config" xsi:type="array"> 
        <item name="visible" xsi:type="boolean">true</item> 
        <item name="label" xsi:type="string" translate="true">Products 
    name</item> 
        </item> 
        </argument> 
        </column>

product.php

<?php
namespace Magneto\SampleRequest\Ui\Component\Listing\Column;  
use \Magento\Catalog\Api\ProductRepositoryInterface;
 use \Magento\Framework\View\Element\UiComponent\ContextInterface;
 use \Magento\Framework\View\Element\UiComponentFactory;
 use \Magento\Ui\Component\Listing\Columns\Column;
 class Products extends Column
   {
  protected $_ProductRepository;
public function __construct(
ContextInterface $context,
 UiComponentFactory $uiComponentFactory,
 ProductRepositoryInterface $ProductRepository,
  array $components = [],
  array $data = [])
   {
   $this->_ProductRepository = $ProductRepository;
    parent::__construct($context, $uiComponentFactory, $components, 
   $data);
    }
     public function prepareDataSource(array $dataSource)
       {
     if (isset($dataSource['data']['items'])) {
    foreach ($dataSource['data']['items'] as $key => $items) {
        $product = $this->_ProductRepository- 
      >getById($items["entity_id"]);
        $dataSource['data']['items'][$key]['entity_id'] = $product- 
       >getName(); //to get product name
       }
      }
       return $dataSource;
      }
       }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.