How to load product collection in Magento 2 and what is the difference between Magento 1 and Magento 2 load product collection
Magento – How to load product collection in Magento 2
magento2object-managerproduct-collection
Related Solutions
So let's go trough this line by line
$_productCollection = Mage::getResourceModel('catalog/product_collection')
->addAttributeToFilter('popular_walldecal', array('eq' => 1))
->setPageSize(8);
Perfect, no need to change this. Just make sure you have the flat product data enabled under System > Configuration > Use Flat Catalog Product
and the attribute popular_walldecal settings Use in product listing
set to yes
$_collectionSize = $_productCollection->count();
Loads the whole collection, I suggest using getSize()
instead of count
since the first doesn't load the collection.
foreach ($_productCollection as $product):
$_product = Mage::getModel("catalog/product")->load($product->getID());
endforeach;
Now here's the real issue. The collection just executes one query over one table (catalog_flat_product_[id]) but loading the product model in the loop executes a whole bunch of queries per iteration. It's better to add the required data to the collection.
$_productCollection = Mage::getResourceModel('catalog/product_collection')
->addAttributeToFilter('popular_walldecal', array('eq' => 1))
->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
->setPageSize(8);
$_productCollection->addFinalPrice()
->addUrlRewrite();
Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection(_productCollection);
Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($_productCollection);
This will
- filter by
popular_walldecal
- add all the attributes in the catalog_flat_product_[id] table
- set the page size to 8
- add the final price and url rewrite data for the product url
- filter the collection by visibility in catalog
- filter the collection by status enabled
And now you're good to go!
addAttributeToSelect('media_gallery')
is not working as media_gallery will come from complicated logic.
You can get easy media_gallery
data inside at loop by loading the full product model But it will take a lot of memory in this case.
Use Backend model of media_gallery
attribute & afterload
of product Model
For getting media_gallery
attribute value without full product load (Mage::getModel('catalog/product')->load($ProductID)) .
You can use afterLoad()
, using this function call backend model
of
media_gallery
eav attribute
load the media image. This is much faster than full model load(Mage::getModel('ctalog/product')->load($ProductID))
foreach($his->getPopularCollection() as $product){
$attributes = $product->getTypeInstance(true)->getSetAttributes($product);
$media_gallery = $attributes['media_gallery'];
$backend = $media_gallery->getBackend();
$backend->afterLoad($product);
$mediaGallery = $product->getMediaGalleryImages();
/* get Image one by using loop*/
foreach ($product->getMediaGalleryImages() as $image) {
echo ($image->getUrl());
}
echo "<br/>..........................................<br/>";
}
Best Answer
The standard collections are actually very similar. There are other similar structures, but for products you can load them in and load them just like Magento 1. Just like in Magento 1, if you are using the collection via iteration you do not have to load it as it's done implicitly.
As always in Magento 2, you should be injecting classes via dependency injection in your class constructor. Plus, if you are going to create more than one instance you can add "Factory" after any class and you'll get a wrapping class that allows you to generate as many as you want.
Here is the use of both together:
With thanks to @megi for the example usage code in their answer.