Magento-how to add custom attributes for the product category

Source: Internet
Author: User

In magento, due to the powerful EAV design method, we can easily add any number of attributes to the product. However, magento does not provide us with the function of adding attributes to product categories. Although we know that the EAV design method used by magento can be fully implemented, how can we add an attribute to the product category of magento? For example, we want to add some attributes based on product categories to apply them to products, or to differentiate product categories.

 

If you do not use magento to operate the database directly, follow these steps:

 

Step 1: Insert a record into the eav_attribute table. Defines a new property and specifies that the property belongs to the product category. First, find the entity_type_id corresponding to the magento product category (Category entity), and confirmattribute_cod,backend_type,frontend_input,frontend_label,default_value,The value of source_mode. If you are unsure about the value of a field, you can refer to the value of other attributes of a product category to set it.

 

INSERT INTO eav_attribute
(entity_type_id, attribute_code, backend_type, frontend_input, frontend_label, default_value, source_model)
VALUES
(3, 'category_featured', 'int', 'select', 'Featured Category', '', 'eav/entity_attribute_source_boolean');

 

Note: Be sure to confirm the correctEntity_type_id. Do not copy the preceding SQL statement. If you are not familiar with it, you can directly use phpMyAdmin. Try to refer to the values of other properties of product category.

 

Only this sentence adds new attributes to the category, but those created categories do not have these attributes. To make these categories have new attributes, you also need to insert a record into another magento table.

 

Step 2: Insert a record to eav_entity_attribute. Here, entity_type_id is the same as the above, attribute_id is the ID of the newly inserted record above, and sort_order is the sequence number of this attribute in this attribute group. ID of the attribute_set_id attribute set. attribute_group_id is the ID of the attribute group. Similarly, if you cannot fully confirm the value of the corresponding field, you can determine it by referring to the value of other attributes of the product category.

 

Insert into eav_entity_attribute (entity_type_id, attribute_set_id, attribute_group_id, attribute_id, sort_order) values (3, 3, 3, <new attribute ID>, <next sort order>)

 

In this way, you add a new attribute to the product category of magento, and the added category will also add this attribute.

 

How can we get the value of this attribute in the magento template or magento model, helper, and controller class code? Thanks to magento's powerful setter and getter, you can directly use $ category-> getattribute_name () to get the value of this attribute.

 

(Updated: magento 1.6)

In magento 1.6, to add attributes to a category, you also need to insert a record in the catalog_eav_attribute table in addition to the preceding two steps. The first field is the attribute ID.

 

Insert into 'copybagcom'. 'catalog _ eav_attribute '(
'Attribute _ id ',
'Frontend _ input_renderer ',
'Is _ global ',
'Is _ visable ',
'Is _ searchable ',
'Is _ filterable ',
'Is _ comparable ',
'Is _ visible_on_front ',
'Is _ html_allowed_on_front ',
'Is _ used_for_price_rules ',
'Is _ filterable_in_search ',
'Used _ in_product_listing ',
'Used _ for_sort_by ',
'Is _ resumable ',
'Apply _ ',
'Is _ visible_in_advanced_search ',
'Position ',
'Is _ wysiwyg_enabled ',
'Is _ used_for_promo_rules'
)
Values (
'123', null, '1', '1', '0', '0', '0', '1', '1', '0 ', '0', '0', '0', '0', null, '0', '0', '1', '0'
);

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.