3

I have a file that I am using to call all magento products which makes some calculations and then saves the result as an attribute. The issue I am having is that only about half of the products are being called.

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

I have tried a few variations of this such as adding store id filter, calling them based on category and removing the select all part. I am really stumped with this now and I can't figure out why only certain products are called. The only thing I have noticed is that of the products that don't get called, they all seem to be simple products. This isn't to say that no simple products get called though

Additional info

    define('MAGENTO', realpath(dirname(__FILE__)));
    require_once MAGENTO . '/app/Mage.php';
    Mage::app();
  • Are these simple products assigned to the website you are executing this code from? – Sander Mangel Aug 27 '13 at 12:08
  • they are all from the same site – develophper Aug 27 '13 at 12:27
6

Most probably, you have the product flat tables enabled and the flat product index is not up to date. Either reindex everything, or you can jump over the flat tables and read the data directly from the EAV tables like this:

$collection = Mage::getResourceModel('catalog/product_collection')->addAttributeToSelect('*');

This may take longer but should get you all the products.

  • I tried that and also reindexed but there was no difference. I included in the question the code above the query just in case there is an issue with that – develophper Aug 27 '13 at 12:26
  • @ThomasRyan Maybe the products are not associated to your default website – Marius Aug 27 '13 at 12:29
  • I tried it using Mage::app(); instead of Mage::app('default'); which didn't change anything either. They are all in the same database and they are also called the same within the site. It just seems to be a problem when calling it in the php file I made – develophper Aug 27 '13 at 12:58
3

If anyone would have the same problem - you need to call Mage::app('admin'); to load products from all websites.

The difference is in the select that is made. If you call Mage::app() you would have

SELECT 1 AS `status`, `e`.`entity_id`, `e`.`type_id`, `e`.`attribute_set_id` FROM `catalog_product_flat_1` AS `e`

or whatever your default store ID is If you call Mage::app('admin') the query would be

SELECT `e`.* FROM `catalog_product_entity` AS `e`

And your $collection = Mage::getModel('catalog/product')->getCollection(); would have all products from all websites/store/store views

2

Place this line

Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

in your code before

 $collection = Mage::getResourceModel('catalog/product_collection')->addAttributeToSelect('*');
  • 1
    This might be a bad idea, because the behavior can be unpredictable if you run your code on different stores. – Fabian Blechschmidt Mar 11 '14 at 13:53
  • Thanks Fabian !! I was having problem in fetching products and it solves my problem. But i still want to know more about it. It will be great if you can tell more about what difference it actually does ? – Sarvagya Mar 12 '14 at 11:41
  • 1
    you have different scopes in magento magentocommerce.com/knowledge-base/entry/… stackoverflow.com/questions/3362041/… and when you load something scope-related, e.g. products with their scope-related attributes or categories, you get the default values, aka scope default or with ID 0. Therefore you should AT LEAST reset the current store back to the store before the load() of the collection, better use Mage_Core_Model_App_Emulation and even better fix the real problem. – Fabian Blechschmidt Mar 12 '14 at 12:23
  • Fabian, Thank You !! – Sarvagya Mar 13 '14 at 12:58

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.