Question

I write a module which make auto translation of my content. I have a problem with the products images, I need to save only one field : media_gallery.

This is what I do :

public function translateAltProducts($idProduct) {

    $bingModel      = Mage::getModel('autotranslation/bing');
    // For each foreign store
    $allStores = Mage::app()->getStores();
    foreach ($allStores as $_eachStoreId => $val) {
        $_storeId = Mage::app()->getStore($_eachStoreId)->getId();
        if($_storeId!=1) {

            // Load product
            $_product = Mage::getModel('catalog/product')->setStoreId($_storeId)->load($idProduct);

            // Get images
            $gallery = $_product->getData('media_gallery');

            // For each image
            foreach($gallery['images'] as &$image) :

                // Do translation
                $translation = false;
                $nbr = 0;

                do {
                    $translation = $bingModel->translateTitle($image['label'], $this->fromLanguage, $_storeId);
                    if(++$nbr==3) die('erreur ' . $idProduct);
                }
                while($translation===false);

                // If got translation
                $image['label'] = $translation;

            endforeach;

            // Save translation
            $_product->addAttributeUpdate('media_gallery', $gallery, $_storeId);

        }
    }
}

I usualy use "$_product->addAttributeUpdate()" to update a field alone but it don't work here.

If I use $_product->setData('media_gallery', $gallery) & $_product->save() it works but I lost all pre-filled fields with "Use Default Value".

If you have any ideas...

Thanks,

Aurelien

Was it helpful?

Solution

It finally works :

$idProduct = 13344;
$_storeId = 2;
$mediaApi = Mage::getModel("catalog/product_attribute_media_api"); 
$_product = Mage::getSingleton('catalog/product')->setStoreId($_storeId)->load($idProduct);
$items = $mediaApi->items($_product->getId());
foreach($items as $item) {
    $item['label']    = 'My cool translation';
    $mediaApi->update($_product->getId(), $item['file'],$item, $_storeId);
}

Thanks stackexchange

OTHER TIPS

I suggest you to try using Singleton for accessing the catalog/product model so that there will be no chance of losing the earlier data which you have saved.

$_product = Mage::getSingleton('catalog/product')->setStoreId($_storeId)->load($idProduct);

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top