Adding new category attribute in Magento
In this article we would like to show you how to add a new custom category attribute. Let’s say, this attribute is needed to display some content on the category page.
First of all, we need to create a new module for adding custom category attribute, we will call it “Custom Category Attribute”.
Step 1. Create new module
We should let Magento know about our new module. Initial configuration file is located in ‘app/etc/modules/Atwix_CustomCategoryAttribute.xml’.
Atwix_CustomCategoryAttribute.xml
HTML Code:
<?xml version="1.0"?>
<config>
<modules>
<Atwix_CustomCategoryAttribute>
<active>true</active>
<codePool>community</codePool>
</Atwix_CustomCategoryAttribute>
</modules>
</config>
It means that the module is active and it is located in the community code pool.
Step 2. Configure module
Module configuration file is located in ‘app/code/<code pool>/<name space>/<module name>/etc’ and its name is config.xml – note, that in our case this looks like ‘ app/code/community/Atwix/CustomCategoryAttribute/etc/config.xml’.
config.xml
HTML Code:
<?xml version="1.0"?>
<config>
<modules>
<Atwix_CustomCategoryAttribute>
<version>0.0.1</version>
</Atwix_CustomCategoryAttribute>
</modules>
<global>
<resources>
<add_category_attribute>
<setup>
<module>Atwix_CustomCategoryAttribute</module>
<class>Mage_Catalog_Model_Resource_Eav_Mysql4_Setup</class>
</setup>
<connection>
<use>core_setup</use>
</connection>
</add_category_attribute>
<add_category_attribute_write>
<connection>
<use>core_write</use>
</connection>
</add_category_attribute_write>
<add_category_attribute_read>
<connection>
<use>core_read</use>
</connection>
</add_category_attribute_read>
</resources>
</global>
</config>
As you can see, configuration file is not large, there are two nodes only: module version and resources for install script. Install script helps us to create a new attribute. In the node <resources> we defined the class for our install script which will be used for the extension. Working with methods of this class helps to create, update, remove attribute (etc). And the node <add_category_attribute> says that script must be located in the folder with the same name (in our case path will be ‘app/code/community/Atwix/CustomCategoryAttribute/sql/add_category_attribute’)
Step 3. Create attribute
Another important thing is to create install script file in the folder ‘add_category_attribute’, and the file name depends on the module version, so it looks like ‘mysql4-install-x.x.x.php’, where x.x.x is the version of the module.
mysql4-install-0.0.1.php
PHP Code:
<?php
$this->startSetup();
$this->addAttribute('catalog_category', 'custom_attribute', array(
'group' => 'General',
'input' => 'textarea',
'type' => 'text',
'label' => 'Custom attribute',
'backend' => '',
'visible' => true,
'required' => false,
'visible_on_front' => true,
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
));
$this->endSetup();
As a result, we created a new attribute in the category with label ‘Custom attribute ‘. This attribute is a text area and it must be visible on the frontend. Check this out!
Step 4. Check result
Here, let’s clear cache and after this, go to Manage Categories – in ‘General’ tab you will see a new attribute: ‘Custom attribute’.
http://www.atwix.com/wp-content/uplo...egory_attr.png
Moreover, to see the content from ‘Custom attribute’ on the frontend in the category page – you need to use the helper for reading attribute in the template of this page (‘app/design/frontend/<package> /<theme>/template/catalog/category/view.phtml’), check please the following code:
PHP Code:
...
<?php if($_customAttribute = $this->getCurrentCategory()->getCustomAttribute()): ?>
<?php echo $_helper->categoryAttribute($_category, $_customAttribute, 'custom_attribute') ?>
<?php endif; ?>
...
As this code was put before ‘product list’ output, so after refreshing a page we will see the result:
http://www.atwix.com/wp-content/uplo...og_product.png
Following all these steps, it will be great to enable wysiwyg editor for a new attribute, that will give us an opportunity to place different content with an inline styles in simple way.
Step 5. Update attribute
For each version of the module, it is also possible to have upgraded scripts which filenames are of the form mysql4-upgrade-0.0.1-0.0.2.php. It seems to be the best solution to update the attribute.
mysql4-upgrade-0.0.1-0.0.2.php
PHP Code:
<?php
$this->startSetup();
$this->addAttribute('catalog_category', 'custom_attribute', array(
'group' => 'General',
'input' => 'textarea',
'type' => 'text',
'label' => 'Custom attribute',
'backend' => '',
'visible' => true,
'required' => false,
'wysiwyg_enabled' => true,
'visible_on_front' => true,
'is_html_allowed_on_front' => true,
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
));
$this->endSetup();
As well, method addAttribute can not only create, but also update the attribute – that’s why, you may also use method updateAttribute.
Step 6. Check updated result
At last, we clear cache and see the button ‘ WYSIWYG Editor ‘ near ‘Custom attribute’. Also, it is necessary to use editor to change the content in this attribute, insert some image, format text and at the end – to save the changes. After all, you should refresh category page and see the result:
http://www.atwix.com/wp-content/uplo...uct_update.png
We hope this article will help you to work with Magento development. Thanks for reading!
BUY REAL PASSPORT ONLINE https://worldpassporte.com/
https://worldpassporte.com/ Do you urgently need a valid European passport, Driver’s license, ID, Residence Permit, toefl – ielts certificate and ….. in a couple of days but Not ready to go through the long stressful process? IF “YES ” you found yourself a solution as our service includes the provision of valid EU Passport, drivers licenses, IDs, SSNs and more at good rates.
https://worldpassporte.com/buy-drivers-license-online/
https://worldpassporte.com/buy-passport-online/
We make it easier for everyone to acquire a registered EU international passport, driver's license, ID Cards, and more regardless of where you are from https://worldpassporte.com/buy-a-cla...ivers-license/
Visit the website: https://worldpassporte.com/
BUY DRIVER LICENSE
https://worldpassporte.com/buy-canad...ssport-online/
https://worldpassporte.com//buy-a-sw...ivers-license/
https://worldpassporte.com/buy-a-us-passport-online/
https://worldpassporte.com//buy-pass...ssport-online/
BUY REGISTERED PASSPORT ONLINE https://worldpassporte.com/
BUY PASSPORT ONLINE https://worldpassporte.com/
BUY DRIVER'S LICENSE ONLINE https://worldpassporte.com/
BUY REAL PASSPORT ONLINE https://worldpassporte.com/
WhatsApp contact: +44 7459 562803
https://worldpassporte.com/buy-real-...icense-online/
https://worldpassporte.com/buy-canad...ssport-online/
https://worldpassporte.com/buy-drivers-license-online/
https://worldpassporte.com/buy-uk-passport-online/
https://worldpassporte.com/buy-german-passport-online/
https://worldpassporte.com/buy-portu...ssport-online/
https://worldpassporte.com/buy-spanish-passport/
https://worldpassporte.com/buy-australian-passport/
https://worldpassporte.com/buy-italian-passport/
https://worldpassporte.com/about-us/
https://worldpassporte.com/services/
https://worldpassporte.com/buy-toefl-ielts-online/
https://worldpassporte.com/buy-dual-citizenship-online/
https://worldpassporte.com/buy-id-card-online/
Buy Drivers License Online
Buy Registered Drivers License Online
Buy Real Drivers License Online
Buy Fake Drivers License Online
Buy Passport Online
Buy Registered Passport Online
Buy Real And Fake Registered Passport Online
Buy Registered IELTS | TOEFL Certificate Online
https://worldpassporte.com/buy-passport-online/
Buy United States passports online
Buy Finland Passport online
Buy UK passports online
Buy a German passport online | German passport application
German passport online
https://worldpassporte.com/buy-drivers-license-online/
Buy the United States Driver's License Online
Buy Finland driving license online
Buy UK driver's license online
Buy a German driver's license online
Buy German driver's license online | German driving license
Buy New Zealand driver's license online
Buy Latvian driver's license online
Buy Ireland driver's license online
Buy New Zealand passport online Buy
Latvia passport online
Buy Ireland Passport online
Buy Denmark Passport online
Buy Portugal Passport online
https://worldpassporte.com/buy-passp...ssport-online/
Buy Portuguese Passport Online | Buy Real And Fake Portuguese Passport Online
Buy Cyprus Passport for sale
Buy Luxembourg Passport online
Buy Turkish passport online
Buy Dutch passport online
Buy Norwegian passport online
Buy Poland Passport online
Buy Romania Passport online
Buy Serbian passport online
Buy Singapore Passport online
Buy Greece Passport online Buy
Hungarian passport online
Buy a French passport online Buy
US passport online buy
Romanian passport online
Buy Slovenian passport online
Buy Slovak passport online Buy
Spanish passport online
Buy a Swiss passport online
Buy Thailand passport online
Buy Chile passport online
Buy Croatian passport online
Buy Canadian passport online
Buy Costa Rica passports online
Buy Estonian passport online
Buy Australia Passport online
Buy Denmark driving license online
Buy Portugal driving license online
Buy Cyprus Driver License For Sale
Buy Luxembourg driver's license online
Buy Turkish driver's license online
Buy Dutch driver's license online
Buy Norwegian driver's license online
Buy Romania driver's license online
Buy Serbian driver's license online
Buy Australian driver's license online
Buy a German passport online
German passport application
https://worldpassporte.com/norway-drivers-license/
https://worldpassporte.com/buy-germa...icense-online/
https://worldpassporte.com/buy-uk-dr...icense-online/
https://worldpassporte.com/buy-austr...e-online-2021/
Contact WhatsApp: +44 7459 562803
BUY DRIVER LICENSE
We use real sophisticated materials for creating documents.
Whether real or fake documents, we use the same quality materials.
The same materials that are used by all the authorities are the same materials that we use to create real documents.
So everything will be 100% of the highest quality.