2014-05-09 64 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方法,则会丢失预填充字段,如果要禁用该产品,则必须在每个视图中进行操作。而且,这是不可能的,它在商店上犯错误。 –