2

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

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
        )

)
  • hi @Qaisar, can we use addFieldToFilter with sku instead of entity_id – jafar pinjar Mar 25 at 7:32
2

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());
?>

Your Answer

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

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