2013-08-28 50 views
6

我被卡住的圖片沒有與他們的產品一起顯示。當我通過(管理產品)手動創建產品並使用該產品插入圖像時,圖像會顯示出很好的圖像。 但是我們有超過1000個需要上傳的產品圖片(所有產品信息已經上傳,我只需要添加圖片) 我導出了產品的CSV文件,工作映像地址是/ n/g/image1 .jpg 然後,我複製了該圖片,但將圖片更改爲image2.jpg,image3.jpg等。然後,我將新圖片上傳到服務器上的該文件夾目錄中,認爲這已足夠。 然後,我上傳CSV文件,打開緩存並重新索引數據,但沒有顯示新圖像,更糟的是工作圖像不顯示。 我已在CSV中填入圖片,small_image和縮略圖詳情。所有圖像大小正確等。Magento導入圖片

更多可以告訴我在哪裏我可以只有1個圖像文件夾在我的所有圖像存儲的目錄? 感謝

我使用的Magento版本1.7.0.2

+0

成癮細節我沒有媒體/導入,我可以看到許多人在他們的目錄中有。 – user1035650

回答

14

首先,你的形象應該media>import目錄存儲

然後在您的CSV文件只是圖像列/imagename.jpg寫

它會在導入目錄中找到相同的圖像名稱,如果圖像存在,則會上傳圖像

編輯

如果你沒有進口目錄,然後簡單地創建它

+1

如果我沒有媒體/導入,我可以簡單地在目錄中創建它?我有媒體然後/目錄/客戶等沒有一個導入文件夾,因爲我沒有設置。然而,var/import卻包含了我導入的CSV文件。 – user1035650

+0

你應該創建文件夾,如果不是的話 –

+2

謝謝你一百萬 –

3

我很抱歉地說,但有更多的事情在幕後與它乾脆把文件名在數據庫Magento的圖像並鏈接到您的圖像。單獨的緩存生成相當複雜。我相信你會很難按照你嘗試的方式去做。

這就是說,我確實有一個建議。由於您的圖像已經在服務器上,我建議您編寫一個簡單的php腳本來告訴magento將它們附加到產品圖像上。這可能是自動化的,但我下面給你一個小例子

要附加的圖像,只需將導航到URL這樣 http://yoursite.com/imageattacher.php?sku=YOURSKU&image=yourimagefilename.jpg

該腳本會是這樣.. 。在你的magento ROOT中創建一個文件,並將其命名爲imageattacher.php。將您的圖像上傳到magento媒體導入目錄。我沒有測試過,但它應該可以工作。

<?php 
    // Initialize magento for use outside of core 
    umask(0); 
    require_once 'app/Mage.php'; 
    Mage::app('admin'); 

    // Get the variables from the URL 
    $sku = $_GET["sku"]; 
    $imageName = $_GET["image"]; 

    // get the image from the import dir 
    $import = Mage::getBaseDir('media') . DS . 'import/' . $imageName; 

    // Load the product by sku 
    $product = Mage::getModel('catalog/product')->loadByAttribute('sku',$sku); 

    // if the product exists, attempt to add the image to it for all three items 
    if ($product->getId() > 0) 
    { 
     // Add the images and set them for their respective usage (the radio button in admin) 
     $product->addImageToMediaGallery($import,array('image', 'small_image', 'thumbnail'),false,false); 

     // Make the changes stick 
     $product->save(); 
    } 

?> 

看到這個http://www.danneh.org/2012/05/creating-products-programmatically-magento/

+0

謝謝,如果它的最後一個選項,我會嘗試這個。 – user1035650

0

最終的問題是,有沒有媒體目錄下創建一個導入文件夾。圖像被上傳到這個文件夾。 CSV中的圖像列已更改爲/image1.jpg,允許它們在網站上顯示。

0

我不得不導入一大堆最近被SKU命名的Magento產品圖片(例如123456.jpg)。這是我用來導入它們的腳本,其中一部分基於CarComp的答案。它僅適用於數字SKU(例如123456),但可以很容易地修改以適應字母數字SKU。

<?php 
require_once(__DIR__ . "/app/Mage.php"); 
Mage::app('admin'); 

class Sku_ImageLoader { 
    private $_uploadDir; 
    private $_imagePaths; 

    public function setUploadDirectory($dir = null) { 
     if (empty($dir)) { 
      if (isset($this->_uploadDir)) return $this; 
      $dir = 'upload';    
      } 
     $this->_uploadDir = Mage::getBaseDir('media') . DS . $dir; 
     // mkdir($this->_uploadDir . DS . "/loaded", 0770); 
     return $this; 
    } 

    public function load() { 
     $this->setUploadDirectory(); 

     // Match product images like 123456.jpg 
     $pattern = '[0-9][0-9][0-9][0-9][0-9][0-9].{jpg,gif,png}'; 

     chdir($this->_uploadDir); 
     $this->_imagePaths = glob($pattern, GLOB_BRACE); 
     return $this; 
    } 

    public function showFiles() { 
     $this->load(); 
     echo "\r\n\r\nSorry, I wasn't able to upload the following image files in " . $this->_uploadDir . "\r\n\r\n<pre>\r\n"; 
     print_r($this->_imagePaths); 
     return $this; 
    } 

    private function _addImage($path) { 
     $sku = (string)intval($path); 
     try { 
      echo "Loading SKU: {$sku} ... "; 
      $product = Mage::getModel('catalog/product')->loadByAttribute('sku',$sku); 
      // if the product exists, attempt to add the image to it for all three items 
      if (strpos(get_class($product), 'Catalog_Model_Product') !== false && $product->getId() > 0) { 
       $product->addImageToMediaGallery($path,array('image', 'small_image', 'thumbnail'),false,false); 
       $product->save(); 
       echo " ok \r\n"; 
       return true; 
      } 
     } catch (Exception $e) { 
      echo "Exception thrown for {$sku}: " . $e->getMessage() . "\r\n"; 
     } 
     echo " (FAILED) \r\n"; 
     return false; 
    } 

    private function _moveImage($path) { 
     // rename($path, 'loaded' . DS . $path); 
     unlink($path); 
    } 

    public function import() { 
     echo "<pre>\r\n"; 
     if (!isset($this->_imagePaths)) $this->load(); 
     foreach ($this->_imagePaths as $path) { 
      if ($this->_addImage($path)) { 
       $this->_moveImage($path); 
      } 
     } 
     return $this; 
    } 

} 

$u = new Sku_ImageLoader(); 

$u->import()->showFiles(); 
?>