Magento Expert Forum - Improve your Magento experience

Results 1 to 7 of 7

Adding new category attribute in Magento

  1. #1
    Junior Member golddev's Avatar
    Join Date
    Mar 2013
    Posts
    45
    Thanks
    1
    Thanked 6 Times in 2 Posts

    Post 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////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 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 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’.



    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/ //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:



    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:



    We hope this article will help you to work with Magento development. Thanks for reading!

    View more threads in the same category:


  2. #2
    Junior Member Kathy Daunt's Avatar
    Join Date
    May 2013
    Posts
    69
    Thanks
    10
    Thanked 4 Times in 4 Posts

    Default

    Thanks found helpful.

    Thanks

    Magento reporting

  3. #3
    Junior Member
    Join Date
    Feb 2014
    Posts
    13
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    thks for your useful post!

  4. #4
    Junior Member Ocodewire's Avatar
    Join Date
    Jan 2014
    Location
    India
    Posts
    244
    Thanks
    10
    Thanked 16 Times in 15 Posts

    Default

    For the installer scripts in this guide, they should really be revised to make use of this approach to instantiate the configuration option array of new attributes.

    $options = $this->_prepareValues(array( /* your options */ ));
    $this->addAttribute(Mage_Catalog_Model_Category::ENTITY, 'your_attribute_code', $options);

    For more information on what options can all be passed to $this->_prepareValues(), review the following methods:
    Mage_Catalog_Model_Resource_Setup::_prepareValues( )
    Mage_Eav_Model_Entity_Setup::_prepareValues()

  5. #5
    New member
    Join Date
    Mar 2017
    Location
    Afghanistan
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default К &#171;Титанику&#187; з

    у нас на этом интернет-сайте http://people4people.ru/ подобран громадный выбор новостей о туризме.

  6. #6
    Junior Member
    Join Date
    Sep 2015
    Posts
    18
    Thanks
    0
    Thanked 2 Times in 1 Post

    Default

    Make sure your module has a setup script, if you don’t know how to do this, here’s a simple layout. The following code should be placed inside your -block in your config.xml :




    Mypackage_Mymodule
    Mage_Catalog_Model_Resource_Setup


    core_setup




    core_read




    core_write



  7. #7
    New member
    Join Date
    Jul 2017
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Great post! I am actually getting ready to across this information, is very helpful my friend. Also great blog here with all of the valuable information you have. Keep up the good work you are doing here.
    bloxorz

Similar Threads

  1. How To Add A Static Block To A Category Page
    By rocker in forum Webmaster & Administrator
    Replies: 2
    Last Post: 17-08-2017, 10:17 AM
  2. Replies: 1
    Last Post: 09-10-2015, 12:05 PM
  3. Adding Cache Support to Magento Blocks
    By sgdev in forum Magento Speed up, Performance and Optimize
    Replies: 2
    Last Post: 29-05-2014, 11:47 AM
  4. Adding currency to woocommerce?
    By speed2x in forum Wordpress
    Replies: 2
    Last Post: 31-07-2013, 01:47 PM
  5. Filter products by attribute on a Magento CMS page
    By david in forum Programming & Development
    Replies: 1
    Last Post: 18-04-2013, 12:19 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •