0

I am working on product collection. I am using

$product = $productModel->load(42);

I have array of products like :

$productArray = array('1','15','25','35','42');

I want to pass this array in load but it is not working and giving error. How we can multiple entity ids into load for getting all details once.

1
  • Hello! Why don't you just use a foreach on $productArray and load each one of those ID's separately and get whatever information you want to get. – user41363 Sep 15 '16 at 16:12
1

You can get like that:

$productArray = array('1','15','25','35','42');

Try to use addAttributeToSelect in the query and check:

$product_ids = array('1','15','25','35','42');
$collection = Mage::getModel('catalog/product')
    ->getCollection()
    ->addAttributeToSelect('barcode')
    ->addAttributeToSelect('name')
    ->addAttributeToSelect('*')
    ->addAttributeToFilter('entity_id', array('in' => $product_ids));
10
  • But i am not getting details of product like Name , barcode etc after print_r($collection->getData()). – Mathew Nov 4 '15 at 11:00
  • I want to load multiple products details once. – Mathew Nov 4 '15 at 11:02
  • you have to load the product to get the data – Qaisar Satti Nov 4 '15 at 11:05
  • Try : $productArray = array('1','15','25','35','42'); $collection = Mage::getModel('catalog/product')->getCollection() ->addAttributeToFilter('entity_id', array('in' => $productArray)); foreach($collection as $product) { $product = $product->load($product->getId()); } – Abdul Nov 4 '15 at 11:10
  • 1
    @Abdul i dont want to use load in foreach loop. – Mathew Nov 4 '15 at 11:15
0

Please try:

$filters = array(
    'type_id',
    'category_ids',
    'website_ids',
    'name',
    'description',
    'short_description',
    'sku',
    'weight',
    'status',
    'url_key',
    'url_path',
    'visibility',
    'has_options',
    'gift_message_available',
    'price',
    'special_price',
    'special_from_date',
    'special_to_date',
    'tax_class_id',
    'tier_price',
    'meta_title',
    'meta_keyword',
    'meta_description',
    'custom_design',
    'custom_layout_update',
    'options_container',
    'image_label',
    'small_image_label',
    'thumbnail_label',
    'created_at',
    'updated_at',
    'barcode'
);

$collection = Mage::getModel('catalog/product')->getCollection()
    ->addAttributeToFilter('entity_id', array('in' => $productArray));

foreach ($filters as $field) {
    $collection->addAttributeToSelect($field);
}

foreach ($collection as $product) {
    echo $product->getSku();
}
0
0
$collection = Mage::getModel('catalog/product')->getCollection();

// select all attributes
$collection->addAttributeToSelect('*');

// using IN statement,
// i.e. selecting only those items whose ID fall in the given array
$productArray = ['1', '15', '25', '35', '42'];
$collection->addAttributeToFilter('id', ['in' => $productArray]);

foreach ($collection as $eachproduct) {
    $product = $eachproduct->load($eachproduct->getId());
    $product->getDescription();
}
0

If you want to get list of products of a particular products ids then you need to call product collection and filter the collection by array of ids.

Product Collection:

$collection = Mage::getModel('catalog/product')->getCollection();

For filter by array of product ids use function addIdFilter($productArray):

$collection->addIdFilter($productArray);

Then using loop get each product:

foreach ($collection as $eachproduct) {
    echo $eachproduct->getName() . '-' . $eachproduct->getId() . '<br/>';
}
1
  • I am only able to get name and price using this. I want also bar code and description also. – Mathew Nov 4 '15 at 11:27
0

Try to use addAttributeToSelect in the query and check:

$product_ids = array('94', '95', '96');
$collections = Mage::getModel('catalog/product')
    ->getCollection()
    ->addAttributeToSelect('*')
    ->addAttributeToFilter('entity_id', array('in' => $product_ids));

foreach ($collections as $collection) {
    echo $collection->getName();
    echo '<br/>';
}
0

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.