Image Magento类别图像的批量更新
我已经在Magento安装中导入了400多个类别。 每个类别都有多个产品,但没有类别图像 我已经编写了一个脚本来循环遍历每个类别,并返回当前类别中第一个产品的图像的URL 下一步是将返回的产品图像设置为其父类别的图像 我的第一种方法是使用类别ID和类别图像URL格式化CSV,以便与MAGMI一起使用,但是在检查了网站上的文档后,我找不到任何有关导入类别图像的信息。这对玛格米有可能吗 如果上述操作失败,我的下一个方法是使用PHP以编程方式设置类别图像。 我找到了下面的代码片段,如果MAGMI方法失败,我打算修改它。有人能确认setThumbnail方法的$image参数是否接受完全限定的URL,还是仅引用本地文件路径Image Magento类别图像的批量更新,image,magento,import,thumbnails,categories,Image,Magento,Import,Thumbnails,Categories,我已经在Magento安装中导入了400多个类别。 每个类别都有多个产品,但没有类别图像 我已经编写了一个脚本来循环遍历每个类别,并返回当前类别中第一个产品的图像的URL 下一步是将返回的产品图像设置为其父类别的图像 我的第一种方法是使用类别ID和类别图像URL格式化CSV,以便与MAGMI一起使用,但是在检查了网站上的文档后,我找不到任何有关导入类别图像的信息。这对玛格米有可能吗 如果上述操作失败,我的下一个方法是使用PHP以编程方式设置类别图像。 我找到了下面的代码片段,如果MAGMI方法失
$category = Mage::getModel('catalog/category')->load($cat);
$category->setImage($image);
$category->save();
非常感谢所有的投入 好-我无法找到使用MAGMI导入类别图像的解决方案。 相反,我找到了一个PHP解决方案:
<?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”语句将随机选择一个类别产品。应该注意的是,这将大大增加脚本执行时间