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 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".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With