Many a time I have seen code where coders are doing all the product manipulation and work within PHP and not letting Magento do the work. Now, there is a caveat here that depending on the data you’re using, this could actually be quicker, but I think for the majority of cases, this is just lack of understanding.
So, I thought I’d knock up a quick post with some pointers on using the Magento Product Collection to simplify and speed up the data you’re getting.
The Product Collection class in Magento is a Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection. It should, in theory always return an array of product objects. The product objects it returns however are not the full product object you might be expecting, but it depends how you want to manipulate the data that comes back. Lets take a look at some of the functions we can use against it to get data.
Getting all products
1 2 |
$collection = Mage::getResourceModel('catalog/product_collection') ->setStoreId($this->getStoreId()); |
So, how can we loop through this?
1 2 3 4 5 |
foreach ($collection as $product) { // you may want to load the full product object - you can do this from the ID $_product = Mage::getModel('catalog/product')->load($product->getId()); // do something } |
Filtering by status
This can be done in two ways:
1 2 |
$collection = Mage::getResourceModel('catalog/product_collection'); Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($collection); |
or
1 2 |
$collection = Mage::getResourceModel('catalog/product_collection') ->addAttributeToFilter('status', array('eq' => '1')); |
You can actually use this as a basis for filtering any attribute. Simply replace “status” with the attribute you wish to filter by.
Filtering by Visibility
1 2 |
$collection = Mage::getResourceModel('catalog/product_collection'); Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($collection); |
Filtering by Catgeory
1 2 3 |
$category = Mage::getModel('catalog/category')->load(4); // ID of category $collection = Mage::getResourceModel('catalog/product_collection') ->addCategoryFilter($category); |
Including / Excluding products
To do this, you can simply filter by ID. The false can be replaced with true, and means exclude / include.
1 2 |
$collection = Mage::getResourceModel('catalog/product_collection') ->addIdFilter(array(1,2,3),false); |
Filter products from current website only
1 2 |
$collection = Mage::getResourceModel('catalog/product_collection') ->addWebsiteFilter(); |
Filter products from current store only
1 2 |
$collection = Mage::getResourceModel('catalog/product_collection') ->addStoreFilter(); |
Just get product IDs
1 2 |
$collection = Mage::getResourceModel('catalog/product_collection') ->getAllIds(); |
Add category IDs to product object
1 2 |
$collection = Mage::getResourceModel('catalog/product_collection') ->addCategoryIds(); |
Add the tiered pricing for a product to the collection
1 2 |
$collection = Mage::getResourceModel('catalog/product_collection') ->addTierPriceData(); |
Image Credit: Victor Bezrukov
Comment or tweet @douglasradburn