Magento alternative for setCurrentStore()

magento

Solution

I think this happens because you do not change store back to default when you exit from your function. But a better solution is to use emulation environment:

function getImages($store, $v){
    $appEmulation = Mage::getSingleton('core/app_emulation');
    $initialEnvironmentInfo = $appEmulation->startEnvironmentEmulation($storeId); 
    //if $store is a model you can use $store->getId() to replace $storeId
    try {
        //your function code here
    catch(Exception $e){
        // handle exception code here
    }
    $appEmulation->stopEnvironmentEmulation($initialEnvironmentInfo);
}

All the code between environment emulation will act as like magento has set the desired store and outside everything should work normally.

Also please note that your code may throw exceptions so, you should use a try catch statement in order to make sure that last line from function that stop environment emulation will be executed every time.

Problem

I have a public function in my Observer folder. (I use this to browse through images) but the thing is, I don't want to use 'Mage::app()->setCurrentStore()' What would be the alternative to browse through the given store without using setCurrentStore()? ``` function getImages($store, $v){ Mage::app()->setCurrentStore($store); $products = Mage::getModel('catalog/product')->getCollection(); $products->addAttributeToSelect('name'); foreach($products as $product) { $images = Mage::getModel('catalog/product')->load($product->getId())->getMediaGalleryImages(); if($images){ $i2=0; foreach($images as $image){ $i2++; $curr = Mage::helper('catalog/image')->init($product, 'image', $image->getFile())->resize(265).'<br>'; } } } } foreach (Mage::app()->getWebsites() as $website) { foreach ($website->getGroups() as $group) { $stores = $group->getStores(); foreach ($stores as $store) { getImages($store, $i); $i++; } } } ``` PS: If I use setCurrentStore() my admin screws up :-S

Original source