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

5

我尝试使用以下代码在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个回答

1

Here is a working example:

$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

顺便提一下,任何提到"addImageToMediaGallery"的回答都是错误的。该方法仅适用于产品,而非类别。 此外,类别的正确属性名称为"image",而不是"thumbnail"。


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

祝好。


这里的$path是什么意思?它是我的目录的完整路径,实际图像存储在其中,例如:D:images/category/T1.jpg,还是只是一个图像名称,例如:T1.jpg? - Sohil Desai

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();

你觉得呢?


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();

1
我已经查找了函数addImageToMediaGallery,它只在产品模型Mage_Catalog_Model_Product中可用,因此会显示错误。 - Gabriel Spiteri

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接