Magento – How to get url of product full image with multistore

cachemagento-1.9multistoreproductproduct-images

$categories = Mage::getModel('catalog/category')
    ->getCollection()
    ->setOrder('entity_id', 'DESC')
    ->setStoreId($storeId)
    ->addAttributeToSelect('name');

// For each category do
foreach ($categories as $_category) {
    // Get products collection and add attributes to select
    $products = $_category->getProductCollection();

    // For each product do
    foreach ($products as $_product) {
       $storeId = 14;
       $product = Mage::getModel('catalog/product');
       $product->setStore($storeId);
       $product->load($_product->getId());
       if (is_object($product)) {
           $imageObj = Mage::helper('catalog/image')->init($_product, 'image');
           $urlIm = $imageObj->__toString();
           $imageUrl[] = $urlIm;
           var_dump($imageUrl);
           exit;
       }
    }
}

I tried to get url's of cached product full images for store 14, but always receiving url's for store 0.

Best Answer

You can also use Mage_Core_Model_App_Emulation class:

$storeId = 14;
$appEmulation = Mage::getSingleton('core/app_emulation');

# Start environment emulation of the specified store
$initialEnvironmentInfo = $appEmulation->startEnvironmentEmulation($storeId);

$products = Mage::getModel('catalog/product')->getCollection();
foreach ($products as $_product) {
    var_dump($_product->getImageUrl());
}

# Stop environment emulation and restore original store
$appEmulation->stopEnvironmentEmulation($initialEnvironmentInfo);

Note: if you use flat tables, you'll only get image path of enabled products.

Source: http://inchoo.net/magento/emulate-store-in-magento/

Related Topic