2014-05-09 65 views
0

我寫了一個模塊,可以自動翻譯我的內容。我對產品圖片有問題,我只需要保存一個字段:media_gallery。Magento - 只保存產品媒體庫

這是我做的:

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); 

     } 
    } 
} 

我usualy用 「$ _product-> addAttributeUpdate()」 單獨更新字段,但它不會在這裏工作。

如果我使用$ _product-> setData('media_gallery',$ gallery)& $ _product-> save()它可以工作,但我使用「使用默認值」丟失了所有預填充字段。

如果您有任何意見...

感謝,

的Aurelien

回答

0

最後,它的工作原理:

$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); 
} 

感謝stackexchange

0

我建議你使用辛格爾頓訪問的目錄/產品型號,試圖讓不會有失去的機會您保存的早期數據。

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

+0

謝謝您anwser,但它不能正常工作,如果我使用$ _product->保存(),我失去了所有使用「使用默認值」的預編譯字段。例如,我有「狀態」字段,如果我使用save方法,則會丟失預填充字段,如果要禁用該產品,則必須在每個視圖中進行操作。而且,這是不可能的,它在商店上犯錯誤。 –