Pergunta

I'm having a major issue with image updates.

I'm working with this code:

public function __construct(){
...
   Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
   $this->product = Mage::getModel('catalog/product')->loadByAttribute('sku',$this->artnr);
}
/**
 * Update Images
 */
protected function updateImages(){
    //Remove Existing Images to Reupload them
    $mediaApi = Mage::getModel("catalog/product_attribute_media_api");
    try{
        $items = $mediaApi->items($this->product->getId());
        foreach($items as $item){
            $mediaApi->remove($this->product->getId(),$item['file']);
            $unlinkImage = Mage::getBaseDir('media').DS.'catalog'.DS.'product'.$item['file'];
            @unlink($unlinkImage);
        }
    }catch(Exception $e){
        Mage::log($e->getMessage());
    }
    if(count($this->import['shop']['images'])>0){
        //write images into directory
        //and run Import
        $currentimage = 0;
        $mediaAttribute = array(
            'thumbnail',
            'small_image',
            'image'
        );
        if(count($this->import['shop']['images'])>0){
            foreach($this->import['shop']['images'] as $image){
                $file = $this->importDir.$image['image_name'];
                file_put_contents($file,$image['image']);
                if($currentimage===0){
                    $this->product->addImageToMediaGallery($file,$mediaAttribute,false,false);
                }else{
                    $this->product->addImageToMediaGallery($file,null,false,false);
                }
                $currentimage++;
            }
        }
    }
}

When I output the the Media Gallery

Mage::log($this->product->getMediaGallery());

I get this output:

[images] => Array
    (
        [0] => Array
            (
                [file] => /5/d/5de102a28622997e5eba689689878570_5.jpg
                [position] => 1
                [label] => 
                [disabled] => 0
            )

        [1] => Array
            (
                [file] => /C/o/Cookie_4_1.png
                [position] => 2
                [label] => 
                [disabled] => 0
            )

        [2] => Array
            (
                [file] => /p/o/post2_16_1.jpg
                [position] => 3
                [label] => 
                [disabled] => 0
            )

    )

sofar so good. But If I look into the Media Gallery Database I get this:

784|703|244|/5/d/5de102a28622997e5eba689689878570_5.jpg|
785|703|244|/C/o/Cookie.png
786|703|244|/p/o/post2.jpg
787|703|244|/5/d/5de102a28622997e5eba689689878570_5_1.jpg
788|703|244|/C/o/Cookie_1.png
789|703|244|/p/o/post2_1.jpg

so they are stored on double. The Files are only stored once. If I look Into the Backend I see one that exists, one that doesn't one that does...

I don't know where I am mistaking, and hope that anyone can help me.

Foi útil?

Solução

As it seems, the API of Magento 1.7 saves the images on double. Bypassed this by simply checking if the images exists in the filesystem. If no, the entries are removed. Thx for the help.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top