在Magento中以编程方式将图像设置为类别

时间:2011-06-02 14:07:56

标签: image magento set categories

我尝试使用此代码在magento中添加一个类别:

$_cat = new Mage_Catalog_Model_Category();
$_cat->setName($nom);
$_cat->setUrlKey($nom);
$_cat->setIsActive(1);

$parentCategory = Mage::getModel('catalog/category')->load(2);
$_cat->setPath($parentCategory->getPath());             
$mediaAttribute = array ('thumbnail');
$_cat->addImageToMediaGallery($other_file, $mediaAttribute, true, false);
$_cat->save();

它的作品,但没有插入图像,我需要知道以编程方式插入类别图像的正确代码是什么,

谢谢你。

5 个答案:

答案 0 :(得分:1)

好吧我解决了,添加额外数据写:

$data['display_mode'] = 'PRODUCTS_AND_PAGE';
$data['page_layout'] = 'one_column';
$data['thumbnail'] = $path; // path to your image 
$_cat->addData($data);  

然后将您的图片放入YOUR_MAGENTO / media / catalog / category / NAME_OF_IMAGE.jpg

干杯。

答案 1 :(得分:1)

这是一个有效的例子:

$newCategory = Mage::getModel('catalog/category');
$newCategory->setName('Category name');
$newCategory->setUrlKey('category-name');
$newCategory->setIsActive(1);

$parentCategory = Mage::getModel('catalog/category')->load(2);
$newCategory->setPath($parentCategory->getPath());             
$newCategory->setImage('file_name.jpg');
$newCategory->save();

确保您的类别图片' file_name.jpg'上传到media / catalog / category / file_name.jpg

BTW,任何答案都提到" addImageToMediaGallery"是错的。此方法仅适用于产品,不适用于类别。 此类别的正确属性名称是"图像"不是" thumbnail"。

答案 2 :(得分:0)

你也可以做得更干净,更可读:

$parentCategory = Mage::getModel('catalog/category')->load(2);

$newCategory = Mage::getModel('catalog/category');
$newCategory->setName($nom);
$newCategory->setUrlKey($nom);
$newCategory->setIsActive(1);    
$newCategory->setPath($parentCategory->getPath());
$newCategory->addImageToMediaGallery($other_file, array ('thumbnail'), true, false);
$newCategory->setDisplayMode('PRODUCTS_AND_PAGE');
$newCategory->setPageLayout('one_column');
$newCategory->setThumbnail($path); // path to your image 
$newCategory->save();

您怎么看?

答案 3 :(得分:0)

ElGabbu,在您的代码中我收到此错误:

   PHP Fatal error:  Uncaught exception 'Varien_Exception' with message 'Invalid method Mage_Catalog_Model_Category::addImageToMedia                                                 Gallery(Array
    (
        [0] => image.jpg
        [1] => Array
            (
                [0] => thumbnail
            )

        [2] => 1
        [3] =>
    )
    )' in /home/www/magento/lib/Varien/Object.php:569

我的代码谁为我工作:

$_cat = new Mage_Catalog_Model_Category();
$_cat->setName($nom);
$_cat->setUrlKey($nom);
$_cat->setIsActive(1);
$parentCategory = Mage::getModel('catalog/category')->load(2);
$_cat->setPath($parentCategory->getPath());     
$_cat->setIsAnchor(1);
$data['display_mode'] = 'PRODUCTS_AND_PAGE';
$data['page_layout'] = 'one_column';
$data['thumbnail'] = $path;
$_cat->addData($data);                                          
$_cat->save();

答案 4 :(得分:0)

我使用以下基于Mage_Catalog_Model_Product_Attribute_Backend_MediaMage_Catalog_Model_Category_Attribute_Backend_Image的剪辑将图像从外部路径复制到缩略图属性,包括_1后缀(如果需要)。

$attr = 'thumbnail';
$new_file = ...; // absolute path to your file
$category = ...; // your category  model

$path = Mage::getBaseDir('media') . DS . 'catalog' . DS . 'category' . DS;
if (md5_file($new_file) != md5_file($path.$category->getData($attr)) {
    $ioObject = new Varien_Io_File();
    try {
        $ioObject->open(array('path'=>$path));
    } catch (Exception $e) {
        $ioObject->mkdir($path, 0777, true);
        $ioObject->open(array('path'=>$path));
    }
    $destFile = $path . Mage_Core_Model_File_Uploader::getNewFileName($path . basename($new_file));
    $ioObject->cp($new_file, $destFile);
    $category->setData($attr, basename($destFile));
}
相关问题