Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a custom image field to a category in Magento?

Tags:

magento

I'm a newbie with magento. I want to add two custom image fields to a category. I have created a module with installer file to my module:

$installer = $this;
$installer->startSetup();

$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
$entityTypeId     = $setup->getEntityTypeId('catalog_category');
$attributeSetId   = $setup->getDefaultAttributeSetId($entityTypeId);
$attributeGroupId = $setup->getDefaultAttributeGroupId($entityTypeId, $attributeSetId);

$setup->addAttribute('catalog_category', 'image1', array(
    'input'         => 'image',
    'type'          => 'file',
    'group' => 'General',
    'label'         => 'Additional image 1',
    'visible'       => 1,
    'required'      => 0,
    'user_defined' => 1,
    'frontend_input' =>'',
    'global'        => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
    'visible_on_front'  => 1,
));

$setup->addAttributeToGroup(
 $entityTypeId,
 $attributeSetId,
 $attributeGroupId,
 'image1',
 '999'  //sort_order
);

$installer->endSetup();

I can see the image field when editing or adding new category but its not saving to database. How to make it work? Thanks

like image 425
Joonas Avatar asked Jul 30 '12 10:07

Joonas


People also ask

How can I get category custom attribute value in Magento 2?

Try below code: $categoryId = 5; $_objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $object_manager = $_objectManager->create('Magento\Catalog\Model\Category')->load($categoryId); echo "<pre>"; print_r($object_manager->getData()); die('shasha test');

How do I assign a product to a category in Magento 2?

Magento 2 allows store admins to add and remove products from the category under Magento 2 Admin panel > CATALOG > Categories > Products in Categories section.

How do I change categories in Magento 2?

To change the category layout, go to Products -> Categories -> Select your desired Category go to the Design tab and select desired option from the Layout dropdown.


1 Answers

To add a new image attribute for the categories, you need to use those values in your setup :

'type'    => 'varchar',
'input'   => 'image',
'backend' => 'catalog/category_attribute_backend_image',

instead of those ones :

'input' => 'image',
'type'  => 'file',
like image 156
blmage Avatar answered Sep 19 '22 13:09

blmage