2

when I am displaying product name through product id Requested product, doesn't exist an error is coming

<?php
    namespace Magneto\PopupQuestion\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 &$items) {
                $productArr = [];
                $product  = $this->_ProductRepository->get($items["entity_id"]);
                foreach ($product->getAllVisibleItems() as $item) {
                $productArr[] = $item->getName(); //to get product name
            }  
                $items['products'] = implode(' - ', $productArr);
                unset($productArr);
            }
        }
        return $dataSource;
    }
} 
1
  • I have added answer It may help you!!
    – kunj
    Feb 15, 2019 at 5:04

2 Answers 2

2

I have corrected your code Try this code it is working fine for me.

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;
}

AND this code

<column name="entity_id" class="Magneto\PopupQuestion\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</item>
                </item>
            </argument>
        </column>
7
  • @AshishRamchandani, Try this
    – kunj
    Feb 15, 2019 at 5:03
  • 2
    no chnages same error Feb 15, 2019 at 5:03
  • @AshishRamchandani, Please check thoes Entity Id really exist?
    – kunj
    Feb 15, 2019 at 5:05
  • yes it is in database it is stored Feb 15, 2019 at 5:06
  • May I know what you actually doing with code, I mean the requirement?
    – kunj
    Feb 15, 2019 at 5:08
1

In productRepositry get method take first parameter as sku not product_id

please check in ProductRepositoryInterface

public function get($sku, $editMode = false, $storeId = null, $forceReload = false);

here you have passed product_id

$product = $this->_ProductRepository->get($items["entity_id"]);

please change below line:

$product = $this->_ProductRepository->get($items["entity_id"]);

with this code(If you have product sku you can use get method):

$product = $this->_ProductRepository->get($items["sku"]);

otherwise you can get product by id:

$product = $this->_ProductRepository->getById($items["entity_id"]);

5
  • i can't get it is there any change in this code please do it Feb 15, 2019 at 4:57
  • @AshishRamchandani i have added my answer please check Feb 15, 2019 at 4:59
  • Undefined index: sku in /var/www/html/magento3/app/code/Magneto/PopupQuestion/Ui/Component/Listing/Column/Products.php on line 25 error is coming Feb 15, 2019 at 5:01
  • Can you add you ui-component code in question @AshishRamchandani Feb 15, 2019 at 5:04
  • <column name="products" class="Magneto\PopupQuestion\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</item> </item> </argument> </column> Feb 15, 2019 at 5:05

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.