2011-12-10 35 views
19

Tôi phải tạo một đại lý nhập khẩu Magento 1.6.x đơn giản giả sử để tạo/cập nhật sản phẩm và hình ảnh của họ. Ai đó có thể tư vấn cho tôi cách thêm hình ảnh sản phẩm mà không phải sử dụng API Magento không?Magento lập trình thêm hình ảnh sản phẩm

Việc thực hiện api hóa ra là rất nghèo và tôi bắt đầu có một chút thất vọng .. :-(

Tôi đã tìm thấy một số câu hỏi khác liên quan đến vấn đề này, nhưng không ai trong số họ liên quan với việc thêm hình ảnh vào . sản phẩm

Đây là những gì tôi đến với:

$product->setIsMassupdate(true) 
    ->setExcludeUrlRewrite(true) 
    ->setManufacturer($this->addManufacturers(utf8_encode($record[4]))) 
    ->setSku($record[3]) 
    ->setAttributeSetId($this->attribute_set)# 9 is for default 
    ->setTypeId(Mage_Catalog_Model_Product_Type::TYPE_SIMPLE) 
    ->setName(utf8_encode($record[5])) 
    ->setCategoryIds($this->getCategories(array($record[0], $record[1], $record[2]))) # some cat id's, 
    ->setWebsiteIDs(array(1)) # Website id, 1 is default 
    ->setDescription(utf8_encode($record[6])) 
    ->setShortDescription($this->shortText(utf8_encode($record[6]), 150)) 
    ->setPrice($price) # Set some price 
    ->setSpecialPrice($special_price) 
    ->setWeight($record[12]) 
    ->setStatus(Mage_Catalog_Model_Product_Status::STATUS_ENABLED) 
    ->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH) 
    ->setTaxClassId(2)  // default tax class 
    ->setPixmaniaimg($record[10]) 
    ->setStockData(array('is_in_stock' => $inStock, 'qty' => $qty)) 
    ->setCreatedAt(strtotime('now')); 

ai đó có thể giúp tôi với việc thêm hình ảnh trực tiếp mà không cần API

Cảm ơn

Lukas

+0

Phiên bản Magento nào? – benmarks

+0

Magento 1.6 - xin lỗi vì đã báo cáo rằng trong nhận xét ban đầu của tôi .. – Bery

+0

Đối với Magento 2: http://magento.stackexchange.com/questions/140612/magento-2-save-all-product-data-outside-magento-with -images –

Trả lời

38

Tôi đã làm điều này trong Magento 1.6.1. Chỉ cần đặt đường dẫn URL hình ảnh của bạn trong mảng đầu tiên và bạn tốt để đi.

Ngoài ra, hãy xem Mage_Catalog_Model_Product để làm quen với addImageToMediaGallery() và các phương pháp khác mà bạn chắc chắn sẽ cần phải lưu ý trong tương lai.

// Add three image sizes to media gallery 
$mediaArray = array(
    'thumbnail' => $putPathHere, 
    'small_image' => $putPathHere, 
    'image'  => $putPathHere, 
); 

// Remove unset images, add image to gallery if exists 
$importDir = Mage::getBaseDir('media') . DS . 'import/'; 

foreach($mediaArray as $imageType => $fileName) { 
    $filePath = $importDir.$fileName; 
    if (file_exists($filePath)) { 
     try { 
      $product->addImageToMediaGallery($filePath, $imageType, false); 
     } catch (Exception $e) { 
      echo $e->getMessage(); 
     } 
    } else { 
     echo "Product does not have an image or the path is incorrect. Path was: {$filePath}<br/>"; 
    } 
} 
+0

Có cách nào để tạo hình ảnh với Magento không? Tôi đã được cung cấp chỉ với một kích thước của hình ảnh. – Bery

+0

Trước tiên, bạn có cần hình ảnh riêng biệt không? Magento cho phép bạn thiết lập một hình ảnh đơn lẻ như hình thu nhỏ, small_image và kích thước hình ảnh nếu bạn cần. –

+0

Ok. Tôi cần phải sử dụng một hoặc nhiều hình ảnh (tối đa hình ảnh hree cho mỗi sản phẩm) và sau đó sử dụng - hình ảnh đầu tiên làm hình ảnh cơ sở, sau đó là small_image và hình thu nhỏ. Hình ảnh luôn giống nhau (ví dụ: image1.jpg là hình nền, hình thu nhỏ và hình thu nhỏ). – Bery

0
set_time_limit(0); 

ini_set('memory_limit', '4095M'); 

error_reporting(E_ALL); 

ini_set('display_errors', 1); 

require_once '../app/Mage.php'; 

umask(0); 

Mage::setIsDeveloperMode(true); 

$storeID = Mage_Core_Model_App::ADMIN_STORE_ID; 

Mage::app()->setCurrentStore($storeID); 



$destination = Mage::getBaseDir() . '/import/images/' . $image; 

$product->addImageToMediaGallery($destination, array('image', 'thumbnail', 'small_image'), false, false); 

} 

này sẽ thiết lập các hình ảnh cơ bản.

Các vấn đề liên quan