好的 - 我無法找到使用MAGMI的解決方案,它允許我導入類別圖像。 相反,我來到了一個PHP的解決方案:
- 遍歷所有的Magento產品分類
- 從產品保存當前類別
- 更新當前類別的圖像與內的隨機(或規定)圖像在步驟2中保存的圖像。
這裏是整個代碼,其中一些可能在處理類似任務時會發現有用。
<?php
/* require the necessary magento classes */
require_once 'app/Mage.php';
Mage::app('default'); // Default or your store view name.
/* construct a category tree object to traverse */
$category = Mage::getModel('catalog/category');
$tree = $category->getTreeModel();
$tree->load();
$ids = $tree->getCollection()->getAllIds();
$arr = array();
/* loop through each category id */
if ($ids){
foreach ($ids as $id){
getImageForCategory($id);
updateCategoryThumbnail($id);
}
}
function getImageForCategory($id){
$images = array();
$catId=$id; // put your category ID in here
$products = Mage::getModel('catalog/product')->getCollection()
->addAttributeToSelect('image')
->addCategoryFilter(Mage::getModel('catalog/category')->load($catId));
Mage::getSingleton('catalog/product_status')->addSaleableFilterToCollection($products); // Only select products that are salable
$products->load();
foreach($products as $product){
$images[] = $product->getImageUrl();
/* remove the following break if you want to use a random image, otherwise the image of the first product will be used. Using random images will cause the execution time to increase dramatically. */
break;
}
if (sizeof($images) > 1) {
$random_image = array_rand($images, 1);
} else {
$random_image = 0;
}
if($images[$random_image]){
saveImageFromURL($images[$random_image],$id);
}
}
function updateCategoryThumbnail($cat){
$image = $cat . ".jpg";
$catc = Mage::getModel('catalog/category')->load($cat);
$catc->setImage($image); // /media/catalog/category
$catc->save();
}
function saveImageFromURL($imgUrl,$cat){
$fout = fopen('/var/www/vhosts/your-site-folder.com/httpdocs/media/catalog/category/' . $cat . '.jpg', 'w');
$fin = fopen($imgUrl, "rb");
while (!feof($fin)) {
$buffer= fread($fin, 32*1024);
fwrite($fout,$buffer);
}
fclose($fin);
fclose($fout);
}
?>
確保有在所述saveImageFromURL()函數所使用的類別的圖像文件夾足夠寫權限。
如上所述,從getImageForCategory()中刪除'break'語句將隨機選擇一個類別產品。應該指出的是,這將大大增加腳本執行時間。