开发者

set image to category programmatically in Magento

I try to add a category in magento by this code:

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

its works, but the image is not inserted, I need to know what开发者_C百科 is the correct code to insert the category image programmatically,

Thanks u.


ok I solved it , to add additionnel datas write :

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

then put your image in YOUR_MAGENTO/media/catalog/category/NAME_OF_IMAGE.jpg

Cheers.


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

Make sure your category image 'file_name.jpg' is uploaded to media/catalog/category/file_name.jpg

BTW, any answer mentioning "addImageToMediaGallery" is wrong. This method only exist for product, not for category. Also the correct attribute name for category is "image" not "thumbnail".


You could have also done to make it look cleaner and more readable:

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

What do you think?


ElGabbu, in your code i get this error :

   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

that my code who worked for me :

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


I use the following snipped based on Mage_Catalog_Model_Product_Attribute_Backend_Media and Mage_Catalog_Model_Category_Attribute_Backend_Image to copy the image from external path to the thumbnail property including a _1 suffix if needed.

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜