批量更新Magento类别图像

时间:2013-04-24 11:21:33

标签: image magento import thumbnails categories

我已将超过400个类别导入Magento安装。 每个类别都有多个产品,但没有类别图像。

我编写了一个循环遍历每个类别的脚本,并返回当前类别中第一个产品图像的URL。

下一步是将返回的产品图像设置为其父类别的图像。

我的第一种方法是格式化带有类别ID和类别图像URL的CSV以供MAGMI使用,但是在http://sourceforge.net/apps/mediawiki/magmi/index.php?title=Import_categories检查文档后,我无法找到有关导入类别图像的任何信息。这可能与MAGMI有关吗?

如果不能解决上述问题,我的下一个方法是使用PHP以编程方式设置类别图像。 如果MAGMI方法失败,我找到了我打算修改的以下代码片段。任何人都可以确认setThumbnail方法的$ image参数是否接受完全限定的URL,或者这仅仅是指本地文件路径?

$category = Mage::getModel('catalog/category')->load($cat);
$category->setImage($image);
$category->save();

所有输入非常感谢!

1 个答案:

答案 0 :(得分:3)

好的 - 我无法找到使用MAGMI的解决方案,允许我导入类别图像。 相反,我得到了一个PHP解决方案:

  1. 浏览所有Magento产品类别
  2. 保存当前类别中产品的随机(或定义)图像
  3. 使用步骤2中保存的图像更新当前类别图像。
  4. 以下是完整的代码,如果处理类似的任务,你们中的一些人可能会觉得有用。

    <?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);
    }
    

    &GT;

    确保对saveImageFromURL()函数中使用的类别图像文件夹有足够的写入权限。

    如上所述,从getImageForCategory()中删除'break'语句将随机选择一个类别产品。应该注意,这将大大增加脚本执行时间。