3

我的观察者文件夹中有一个公共函数。(我用它来浏览图像)但问题是,我不想使用 'Mage::app()->setCurrentStore()'

在不使用 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:如果我使用 setCurrentStore() 我的管理员搞砸了:-S

4

2 回答 2

11

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.

于 2013-02-24T19:38:52.223 回答
6

您可以为此使用仿真。来自http://incho.net/ecommerce/magento/emulate-store-in-magento/

$appEmulation = Mage::getSingleton('core/app_emulation');
//Start environment emulation of the specified store
$initialEnvironmentInfo = $appEmulation->startEnvironmentEmulation($storeId);
/*
 * Any code thrown here will be executed as we are currently running that store
 * with applied locale, design and similar
 */
//Stop environment emulation and restore original store
$appEmulation->stopEnvironmentEmulation($initialEnvironmentInfo);

I would like to point out that you are making a resource-expensive decision when you call load() in the collection iteration. It may be ok given your catalog size and running context, but you can accomplish the same without thrashing your database by calling $products->addAttributeToSelect('*'). This will grab all attributes and values though; given current case, you could get what you need as follows:

$products = Mage::getModel('catalog/product')->getCollection();
$products->addAttributeToSelect('name')
         ->addAttributeToSelect('media_gallery');
foreach($products as $product) {
    $images = $product->getMediaGalleryImages();
    if($images){
        // your logic/needs
    }
}
于 2013-02-24T19:36:00.480 回答