magento — 如何為商品分類(category)添加自訂屬性

來源:互聯網
上載者:User

在magento中,由於使用了強大的EAV設計方法,我們可以很方便的給商品添加任意數量的屬性。然而magento沒有給我們提供給商品分類添加屬性的功能。儘管我們知道magento所採用的EAV設計方法是完全可以實現的,但是我們又該如何才能給magento的商品分類添加一個屬性呢?比如我們想基於產品分類添加一些屬性使之應用於產品,或者用來區分產品分類等。

 

如果不通過magento的方式,直接通過操作資料庫,可以按照以下步驟來添加:

 

step 1,向eav_attribute表插入一條記錄。作用是定義一個新屬性,並指定這個屬性屬於商品分類category。先找出magento商品分類(category entity)對應的entity_type_id,並確定好attribute_cod,backend_type,frontend_input,frontend_label,default_value,source_mode的值。如果不確定某個欄位應該使用什麼值,可以參考一個商品分類其它屬性的值來設定。

 

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');

 

注意:一定要確認正確的entity_type_id,不要照搬上面的sql語句,如果不太熟悉可以直接使用phpmyadmin,盡量參照商品分類其它屬性的值。

 

僅僅這一句只是給分類添加了新增的屬性,但是那些已經建立的分類是不會有這些屬性的,為了讓這些分類有新增的屬性,還需要向magento的另一個表中插入一條記錄。

 

Step 2,向eav_entity_attribute插入一條記錄。其中 entity_type_id和上面得到的是一樣的,attribute_id則是上面新插入記錄的ID,sort_order則是這個屬性在這個屬性群組中排序的序號。attribute_set_id屬性集的ID,attribute_group_id是屬性分組的ID。一樣的,如果你不能完全確認相應欄位的值,可以通過參考商品分類其它屬性的值來確定。

 

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> )

 

這樣你就給magento的商品分類(category)添加了一個新屬性,而且已經添加完的分類也會這個新增屬性。

 

那我們如何,才能在magento模板中,或者magento的model,helper,controller的類代碼中擷取到這個屬性的值呢?得益於magento強大的setter,getter,你可以直接使用$category->getAttribute_name()來擷取這個屬性的值。

 

(更新: magento 1.6版本)

在magento 1.6版本中,要給分類添加屬性,除了上面兩個步驟之外還需要在表catalog_eav_attribute插入一條記錄,第一個欄位為屬性ID

 

INSERT INTO `copybagcom`.`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 (
'127', NULL , '1', '1', '0', '0', '0', '1', '1', '0', '0', '0', '0', '0', NULL , '0', '0', '1', '0'
);

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.