0

I am trying to fetch products extra images for my ebay store.

i am using m2e pro and they have a tag system #media_gallery# is what we use to pull the images but it is not working.

the strange thing is, it is fetching the main product image using tag #image#

i am also using stock in the channel for importing products data which just fetchs the product link instead of full image and shows that in our store.

here is the main code which prepares the images for ebay.

    public function getThumbnailImageLink()
{
    $resource = Mage::getSingleton('core/resource');

    $cacheHelper = Mage::helper('M2ePro/Data_Cache_Permanent');
    $cacheKey = '_thumbnail_attribute_id_';

    if (($attributeId = $cacheHelper->getValue($cacheKey)) === false) {

        $attributeId = $resource->getConnection('core_read')
               ->select()
               ->from($resource->getTableName('eav_attribute'), array('attribute_id'))
               ->where('attribute_code = ?', 'thumbnail')
               ->where('entity_type_id = ?', Mage::getModel('catalog/product')->getResource()->getTypeId())
               ->query()
               ->fetchColumn();

        $cacheHelper->setValue($cacheKey, $attributeId);
    }

    $storeIds = array((int)$this->getStoreId(), Mage_Core_Model_App::ADMIN_STORE_ID);
    $storeIds = array_unique($storeIds);

    $queryStmt = $resource->getConnection('core_read')
          ->select()
          ->from($resource->getTableName('catalog_product_entity_varchar'), array('value'))
          ->where('store_id IN (?)', $storeIds)
          ->where('entity_id = ?', (int)$this->getProductId())
          ->where('attribute_id = ?', (int)$attributeId)
          ->order('store_id DESC')
          ->query();

    $thumbnailTempPath = null;
    while ($tempPath = $queryStmt->fetchColumn()) {

        if ($tempPath != '' && $tempPath != 'no_selection' && $tempPath != '/') {
            $thumbnailTempPath = $tempPath;
            break;
        }
    }

    if (is_null($thumbnailTempPath)) {
        return NULL;
    }

    $imagePathOriginal = Mage::getBaseDir('media').DS.'catalog/product'.$tempPath;

    if (!is_file($imagePathOriginal)) {
        return NULL;
    }

    $width  = 100;
    $height = 100;

    $prefixResizedImage = 'resized-'.$width.'px-'.$height.'px-';
    $imagePathResized   = dirname($imagePathOriginal).DS.$prefixResizedImage.basename($imagePathOriginal);

    $baseUrl = Mage::app()->getStore($this->getStoreId())
                          ->getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA, false);

    if (is_file($imagePathResized)) {

        $currentTime = Mage::helper('M2ePro')->getCurrentGmtDate(true);

        if (filemtime($imagePathResized) + self::THUMBNAIL_IMAGE_CACHE_TIME > $currentTime) {

            $tempValue = str_replace(basename($imagePathOriginal),$prefixResizedImage.basename($imagePathOriginal),
                                     $tempPath);

            return $baseUrl.'catalog/product/'.ltrim($tempValue, '/');
        }

        @unlink($imagePathResized);
    }

    try {

        $imageObj = new Varien_Image($imagePathOriginal);
        $imageObj->constrainOnly(TRUE);
        $imageObj->keepAspectRatio(TRUE);
        $imageObj->keepFrame(FALSE);
        $imageObj->resize($width, $height);
        $imageObj->save($imagePathResized);

    } catch (Exception $exception) {
        return NULL;
    }

    if (!is_file($imagePathResized)) {
        return NULL;
    }

    $tempValue = str_replace(basename($imagePathOriginal),$prefixResizedImage.basename($imagePathOriginal),
                             $tempPath);

    return $baseUrl.'catalog/product/'.ltrim($tempValue,'/');
}

public function getImageLink($attribute = 'image')
{
    if ($attribute == '') {
        return '';
    }

    $imageUrl = $this->getAttributeValue($attribute);
    return $this->prepareImageUrl($imageUrl);
}

public function getGalleryImageLink($position = 1)
{
    $position = (int)$position;

    if ($position <= 0) {
        return '';
    }

    // need for correct sampling of the array
    $position--;

    $galleryImages = $this->getProduct()->getData('media_gallery');

    if (!isset($galleryImages['images']) || !is_array($galleryImages['images'])) {
        return '';
    }

    if (!isset($galleryImages['images'][$position])) {
        return '';
    }

    $galleryImage = $galleryImages['images'][$position];

    if (isset($galleryImage['disabled']) && (bool)$galleryImage['disabled']) {
        return '';
    }

    if (!isset($galleryImage['file'])) {
        return '';
    }

    $imageUrl = Mage::app()->getStore($this->getStoreId())
                    ->getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA, false).
                    'catalog/product/'.ltrim($galleryImage['file'],'/');

    return $this->prepareImageUrl($imageUrl);
}

public function getGalleryImagesLinks($limitImages = 0)
{
    $limitImages = (int)$limitImages;

    if ($limitImages <= 0) {
        return array();
    }

    $galleryImages = $this->getProduct()->getData('media_gallery');

    if (!isset($galleryImages['images']) || !is_array($galleryImages['images'])) {
        return array();
    }

    $images = array();

    $i = 0;
    foreach ($galleryImages['images'] as $galleryImage) {

        if ($i >= $limitImages) {
            break;
        }

        if (isset($galleryImage['disabled']) && (bool)$galleryImage['disabled']) {
            continue;
        }

        if (!isset($galleryImage['file'])) {
            continue;
        }

        $imageUrl = Mage::app()->getStore($this->getStoreId())
                        ->getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA, false).
                        'catalog/product/'.ltrim($galleryImage['file'],'/');

        $imageUrl = $this->prepareImageUrl($imageUrl);

        if (empty($imageUrl)) {
            continue;
        }

        $images[] = $imageUrl;
        $i++;
    }

    return $images;
}

private function prepareImageUrl($url)
{
    if (!is_string($url) || $url == '') {
        return '';
    }

    return str_replace(array('https://', ' '), array('http://', '%20'), $url);
}

the attribute i have in magento for extra images are either "gallery" , "media_gallary" or "small_image"

i have tried changing the code every where above but no use. please help me solve this puzzle.

0

On the understandinge forum exists a similar question, he solved it by quadrupling the php memory limit, but there is no info on the inital value What is your memory limit? Mine is at 768M and it is only working in the preview and for the first views and afterwards it stops working.

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.