2

I tried this code but :-

$product = $this->_objectManager->create('Magento\Catalog\Model\ResourceModel\Product\Collection')
            ->addAttributeToSelect('*')
            ->addAttributeToFilter('entity_id', array('in' => $productIds));


     echo '<pre>'; print_r($product->getData()); echo '<pre/>'; die();

             Array
    (
        [0] => Array
            (
                [entity_id] => 36
                [attribute_set_id] => 11
                [type_id] => simple
                [sku] => 24-MG04
                [has_options] => 0
                [required_options] => 0
                [created_at] => 2016-08-21 07:56:06
                [updated_at] => 2016-08-21 07:56:06
            )
    )

And I am getting this result except of all the attributes.

Qaisar Satti
  • 32,469
  • 18
  • 85
  • 137
Manish Goswami
  • 1,159
  • 9
  • 27

3 Answers3

3

We can also use \Magento\Catalog\Model\ProductFactory $productFactory as a factory object and after setting it in constructor use

$collection = $this->_productFactory->create()->getCollection()
    ->addAttributeToSelect(
        'name'
    )->addAttributeToSelect(
        'sku'
    )->addAttributeToSelect(
        'price'
    )->addAttributeToFilter(
        'entity_id', array('in' => $productIds)
    );

Also refer to this link Magento 2: to use or not to use the ObjectManager directly?

Amit Singh
  • 1,765
  • 3
  • 19
  • 37
0

Inside block file,

<?php

public function getProductInfo(){
         $product = $this->_objectManager->create('Magento\Catalog\Model\ResourceModel\Product\Collection')
        ->addAttributeToSelect('*')
            ->addAttributeToFilter('entity_id', array('in' => $productIds));
  return $product;
}

You have to just call inside your template files, When you can iterate loop inside template file its will display name,

<?php 
$productsData = $block->getProductInfo();
foreach($productsData as $product){
  echo $product->getName();
}
?>
Rakesh Jesadiya
  • 42,221
  • 18
  • 132
  • 183
0

you need to add addAttributeToSelect(array('*')) to get all attributes of product

$product = $this->_objectManager->create('Magento\Catalog\Model\ResourceModel\Product\Collection')
        ->addAttributeToSelect(array('*'))
            ->addAttributeToFilter('entity_id', array('in' => $productIds));
Qaisar Satti
  • 32,469
  • 18
  • 85
  • 137