0

We want to retrieve all products and their stock using a script. This is the code we run. But this shows only the products of the store domain (url) that we use to request the file.

question: How can we remove the (automatic) store filter? and retrieve all the products of the magento installations (just like catalog products overview in backend?

Mage::app();

$data = Mage::getModel('catalog/product')->getCollection()
                    ->addFieldToFilter('visibility', Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH)
                    ->addAttributeToFilter('status', 1)
                    ->addAttributeToSelect('name')
                    ->addAttributeToSelect('type_id')
                    ->addAttributeToSelect('sku')
                    ->addAttributeToSelect('manufacturer')
                    ->addAttributeToSelect('cost')
                    ->addAttributeToSelect('price')
                    ->addAttributeToSelect('stock_status');
1

This should work if you emulate the admin-store like this:

// emulate the admin store to be able to save entities
$appEmulation = Mage::getSingleton('core/app_emulation');
$initialEnvironmentInfo = $appEmulation->startEnvironmentEmulation(Mage_Core_Model_App::ADMIN_STORE_ID);

// get collection here, current store and flat catalog data won't apply

// end admin store emulation
$appEmulation->stopEnvironmentEmulation($initialEnvironmentInfo);
| improve this answer | |
  • yes thanks tried that, no luck ``` Fatal error: Uncaught Error: Call to a member function getBackend() on boolean in pp/code/core/Mage/Eav/Model/Entity/Abstract.php:816 ``` – snh_nl Apr 18 '16 at 15:46
  • I have the idea that ->addFieldToFilter('visibility', Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH) is auto-adding the storeid filter as well (but how it knows which I am in is a mystery) – snh_nl Apr 18 '16 at 15:46
  • @your error: I am not sure, but this looks as if you are filtering or selecting an attribute that doesn't exist. I assume "stock_type" is one of your custom attributes, do you also have "stock_status"? Also, can you try with "addAttributeToFilter('visibility....." instead of "addFieldToFilter(..."? – simonthesorcerer Apr 18 '16 at 15:48
  • thanks this was it! Only now - how do I add the filter "stock_type" again? ->addAttributeToFilter('stock_type', array('like' => '%Stock Article%')) – snh_nl Apr 18 '16 at 15:57
  • What is "stock_type"? I don't think it is Magento standard? – simonthesorcerer Apr 18 '16 at 16:02

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.