12

I am trying to get the entire magento product collection, without any filters or restrictions, but I fail to get all products.

I've tried various methods already, but they all give me a very limited selection of products. Let's say the store contains 5000 products, but it only shows 500. When I check the catalog -> products is does show me the entire list.

Mage::getModel('catalog/product')->getCollection();
Mage::getResourceModel('catalog/product_collection')->addAttributeToSelect('*');
Mage::getModel("catalog/product")->getResourceCollection()->load();

All of them return the same amount (500), while I expect it to give me 5000 products. I would prefer not to use Zend or PHP and just stick to the Magento way to get them.

Does anyone know how to really get ALL products or can point me in the right direction why this isn't working?

The select-string that is returned is:

SELECT 1 AS `status`, `e`.`entity_id`, `e`.`type_id`, `e`.`attribute_set_id` FROM `catalog_product_flat_4` AS `e`
JNDPNT
  • 7,445
  • 2
  • 34
  • 40
  • I presume that the 'missing' products are from a different store? By default the catalog collections filter by store id. I'm not sure you can override this, since attributes can be set on a store basis, this is how it decides which ones to pull back for the model. Looping through your stores and fetch the products for each is possibly your best chance of getting all products. – Peter O'Callaghan Jan 16 '12 at 14:15
  • Actually they aren't all... When I go to catalog products and I filter by store I still have more products (say 3000). If I additionally filter on enabled/disabled it gives me 10 products from the 500 returned. So I still 'miss' 10 products, even when these filters are set by default. – JNDPNT Jan 16 '12 at 14:20
  • 1
    Retrieve the database query with `echo (string) Mage::getModel('catalog/product')->getCollection()->getSelect();` and add that to your question, that could help things. – clockworkgeek Jan 16 '12 at 16:07
  • I've added the select string to the question on your request. – JNDPNT Jan 16 '12 at 16:12
  • In which store context is this running? – benmarks Jan 16 '12 at 17:09
  • Not the default store, but in another store view. – JNDPNT Jan 17 '12 at 07:31
  • The `SELECT` statement you've shown definitely selects all records, but for `core_store.store_id == 4` only, of course. Does `SELECT COUNT(*) FROM catalog_product_flat_4` executed by non-Magento (e.g. phpMyAdmin) match `echo count(Mage::getModel('catalog/product')->getCollection());` when executed in the context of store_id #4? – Jürgen Thelen Jan 17 '12 at 09:02
  • That the same... I figured out that these are products that are enabled, but I would like a collection of all products, enabled or disabled using default magento. I guess this isn't possible? – JNDPNT Jan 17 '12 at 09:33
  • Was this resolved? Iam having the same, with 320 product returned when I filter by configurable. – Theodores Oct 31 '12 at 09:28
  • Sort of, in my case it was because I was looking in the flat tables but in fact I needed all products. If you need the same you should write a query on the non-flat table for what you need. – JNDPNT Oct 31 '12 at 10:31

6 Answers6

15
//to overwrite limit but you need first to increase your memory limit

 $collection = Mage::getModel('catalog/product')->getCollection()
->addAttributeToSelect('*') // select all attributes
->setPageSize(5000) // limit number of results returned
->setCurPage(1); // set the offset (useful for pagination)

// we iterate through the list of products to get attribute values
foreach ($collection as $product) {
  echo $product->getName(); //get name
  echo (float) $product->getPrice(); //get price as cast to float
  echo $product->getDescription(); //get description
  echo $product->getShortDescription(); //get short description
  echo $product->getTypeId(); //get product type
  echo $product->getStatus(); //get product status

  // getCategoryIds(); returns an array of category IDs associated with the product
  foreach ($product->getCategoryIds() as $category_id) {
      $category = Mage::getModel('catalog/category')->load($category_id);
      echo $category->getName();
      echo $category->getParentCategory()->getName(); // get parent of category
  }
  //gets the image url of the product
  echo Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA).
      'catalog/product'.$product->getImage();
  echo $product->getSpecialPrice();
  echo $product->getProductUrl();  //gets the product url
  echo '<br />';
}
ashraf mohammed
  • 1,322
  • 15
  • 20
10

And something like this:

$products = Mage::getModel('catalog/product')->getCollection();
foreach($products as $prod) {
$product = Mage::getModel('catalog/product')->load($prod->getId());
}

With this method I get more than 500 but all my product...

Alexandre
  • 3,088
  • 3
  • 34
  • 53
  • 1
    Same thing. It's not that the products aren't being loaded, it's that they are not present in the collection... That what isn't present can't be loaded of course :-). – JNDPNT Jan 16 '12 at 13:41
  • 1
    Is it not bad practice to `load()` inside a loop? – scrowler Jan 26 '15 at 19:39
  • I can hardly answer to you because I didn't touch to Magento since more than 1 year. how else would you like to do? May be you could collect all the ids and then request for this ids but I don't remember if it's possible. I was using this loop for an internal script, so performance issue was not important in my case. – Alexandre Jan 27 '15 at 06:23
5

Several possibilities here: 1. Some inner limitation, like 500 at all. 2. Some paging limitation. Products per page(in db abstract) 3. Some lazyload limitation.

Perhaps, there is some over problem, but I think this is some inner limit.

Jevgeni Smirnov
  • 3,787
  • 5
  • 33
  • 50
  • 1
    It isn't because the "500" was just a fictional number to abstract things. In reality it's something like 334 which is not exactly a limitation number. Thanks for thinking with me though :). I think Alexandre is partially right, but probably there are more default filters which are applied. – JNDPNT Jan 16 '12 at 14:58
2

Turn off your flat_catalog_product in admin > system > configuration > catalog > catalog. After this you will get a full product collection. Even though this is a workaround, it helped me to achieve what I needed to achieve.

Joey Gomes
  • 66
  • 3
1

You might be using Flat Catalog Product Structure . This create separate table for each store view.

Use Code below to print sql query . you will see the collection is coming from flat table like

catalog_product_flat_38

echo Mage::getModel('catalog/product')->getCollection()->getSelect();
0

try using

Mage::app()->setCurrentStore('0');
// same as 
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

this hepled me