博客

Magento 二次开发系列二 — 分类增加自定义属性

【php教程】Magento 二次开发系列二 — 分类增加自定义属性

Magento 后台不支持直接添加分类的新属性,商品是可以添加的,所以就借鉴了商品的逻辑。
新增的属性会存储在表eav_attribute中,这里有个字段要注意下entity_type_id,这个值对应表eav_entity_type中的记录,我们要添加的是分类的属性,所以选择3,看下图

1.然后通过sql脚本创建一条记录:
INSERT INTO `eav_attribute` (`attribute_id`, `entity_type_id`, `attribute_code`, `attribute_model`, `backend_model`, `backend_type`, `backend_table`, `frontend_model`, `frontend_input`, `frontend_label`, `frontend_class`, `source_model`, `is_required`, `is_user_defined`, `default_value`, `is_unique`, `note`)
VALUES(null, 3, 'category_model', NULL, NULL, 'text', NULL, NULL, 'textarea', 'Category Model', NULL, NULL, 0, 0, NULL, 0, NULL);

2.通过 sql:SELECT attribute_id FROM `eav_attribute` where attribute_code='category_model' 这里的attribute_code 这个值是你刚才新建的属性code 得到attribute_id。

3.上面的语句虽然创建了一个属性字段,但是还没有对新建的归属进行设置,这一步就是做这个事情。将步骤2中获得的id 填到下面的sql 语句中,下图中的100 就是上面的语句查询的结果。attribute_group_id 对应的值需要从表eav_attribute_group中查询获取,这里取id为4(General Information) ,sql语句:
INSERT INTO `eav_entity_attribute` (`entity_attribute_id`, `entity_type_id`, `attribute_set_id`, `attribute_group_id`, `attribute_id`, `sort_order`)
VALUES(null, 3, 3, 4, 100, 10);

4.关联分类,并设置参数,sql语句:
INSERT INTO `catalog_eav_attribute` (`attribute_id`, `frontend_input_renderer`, `is_global`, `is_visible`, `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_configurable`, `apply_to`, `is_visible_in_advanced_search`, `position`, `is_wysiwyg_enabled`, `is_used_for_promo_rules`)
VALUES(100, NULL, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, NULL, 0, 1, 1, 0);

5.添加part属性与上面类似,这里就不再展示了

6.网站后台->系统->权限->变量(magento1.9.2.X新增的,之前的版本不需要这个操作)新增两个允许的变量(即我们新增的两个),如果不添加系统将不会加载自定义的属性。
cate03

7.刷新网站缓存 ok 在分类管理页面就可以看到刚才添加的那个属性了。

8.后台分类管理->新增分类页面就可以看到新增的属性了
cate02

9.代码中获取的话就可以像其他属性一样获取即可,比如,$category->getCategoryModel() 或$category->getData(‘category_model’)。

分类添加自定义属性步骤就是以上这些。

注:此文为原创,如转载请注明出处。