閱讀1027 返回首頁    go 阿裏雲 go 技術社區[雲棲]


magento -- 如何為商品分類(category)添加自定義屬性

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的值。如果不確定某個字段應該使用什麼 值,可以參考一個商品分類其它屬性的值來設定。

NSERT 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()來獲取這個 屬性的值。

 

出處:https://blog.csdn.net/xinhaozheng/archive/2009/07/30/4395564.aspx

最後更新:2017-04-02 04:26:01

  上一篇:go magento -- 如何獲得某個屬性組的所有商品
  下一篇:go magento 添加css js