3

I want to load array(2,6,7) of products id's using objectManager and get details of the product. I found most of the Magento-1.9 solutions. Is there anything for Magento 2?

I found this,

<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
/** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $productCollection */
$productCollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\Collection');
/** Apply filters here */
$collection = $productCollection->addAttributeToSelect('*')
            ->load();

foreach ($collection as $product){
     echo 'Name  =  '.$product->getName().'<br>';
}  
?>

but how to load my array product ids?

3 Answers 3

6

Try below code use in

 <?php
    $porIds=array(2,6,7);
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    /** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $productCollection */
    $productCollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\Collection');
    /** Apply filters here */
    $collection = $productCollection->addAttributeToSelect('*')
                ->addFieldToFilter('entity_id', array('in' => $porIds));

                echo '<pre>';print_r($collection->getData());

Output

Array
(
    [0] => Array
        (
            [entity_id] => 2
            [attribute_set_id] => 15
            [type_id] => simple
            [sku] => 24-MB04
            [has_options] => 0
            [required_options] => 0
            [created_at] => 2017-11-03 11:21:57
            [updated_at] => 2017-11-03 11:21:57
        )

    [1] => Array
        (
            [entity_id] => 6
            [attribute_set_id] => 15
            [type_id] => simple
            [sku] => 24-MB02
            [has_options] => 0
            [required_options] => 0
            [created_at] => 2017-11-03 11:21:58
            [updated_at] => 2017-11-03 11:21:58
        )

    [2] => Array
        (
            [entity_id] => 7
            [attribute_set_id] => 15
            [type_id] => simple
            [sku] => 24-UB02
            [has_options] => 0
            [required_options] => 0
            [created_at] => 2017-11-03 11:21:58
            [updated_at] => 2017-11-03 11:21:58
        )

)
1
  • hi @Qaisar, can we use addFieldToFilter with sku instead of entity_id Commented Mar 25, 2019 at 7:32
3

Try this,

<?php 
$objectManager = Magento\Framework\App\ObjectManager::getInstance();
$products = $objectManager->get('Magento\Catalog\Model\Product')
            ->getCollection()
            ->addAttributeToFilter('entity_id',array('product_id1','product_id2'));
echo "<pre>";
print_r($products->getData());
?>
2
1
$porductIds=array(2,6,7);
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

$productCollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\Collection');
    $collectionByIds = $productCollection->addAttributeToSelect('*');
    $collectionByIds->addFieldToFilter('entity_id', array('in' => $porductIds));
    $collectionByIds->load();

    foreach ($collectionByIds as $collection) :
        echo "<pre>";
        print_r($collection->getName());
    endforeach;

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.