明天就過年了,哈哈!
1
如何獲得配置資料
Mage::getStoreConfig('段',Store_id),譬如:
Mage::getStoreConfig('helloworld_options/message/hello_message',1);
可以得到sysytem.xml檔案中配置後,然後再後台system-config裡面設定的值。
2
如何配置資料。
2.1
首先在config.xml中要指明helpers,譬如:
<global>
<helpers>
<helloworld>
<class>Zh_Helloworld_Helper</class>
</hellowordl>
</helpers>
</global>
2.1.1
在配置中必須配置helpers類。
使用helper
----------------------->
Mage::helper('helloworld/foo');
對用的helpers類Zh_Helloworld_Helper_Foo
如果是Zh_Helloworld_Helper_Data可以直接通過Mage::helper('helloworld')得到data協助類;
2.1.2協助類執行個體:
app/code/local/Zh/Helloworld/Helper/Data.php
class Zh_Helloworld_Helper_Data extends Mage_Core_Helper_Abstract
{
}
3
添加新的段(section):
在etc/system.xml檔案中:
<config>
<tabs>
<helloconfig translate="label" module="helloworld">
<label>Hello Config</label>
<sort_order>222</sort_order>
</helloconfig>
</tabs>
<sections>
<helloworld_options translate="label" module="helloworld">
<label>Hello World</labe>
<tab>helloconfig</tab>
<frontend_type>text</frontend_type>
<sort_order>1000</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
</helloworld_options>
</sections>
</config>
4
添加acl許可權:
etc/config.xml
<config>
<adminhtml>
<acl>
<resources>
<admin>
<children>
<system>
<children>
<config>
<children>
<helloworld_options>
<title>Store Hello World Module Section</title>
</helloworld_options>
</children>
</config>
</children>
</system>
</children>
</admin>
</resources>
</acl>
</adminhtml>
</config>
分析這個代碼,所有的代碼定義在如下:
<adminhtml>
<acl>
<resources>
</resources>
</acl>
</adminhtml>
5
添加組:
在magento中,選項是通過組來劃分的,所以在添加選項之前得先添加組,修改system.xml檔案
<sections> <helloworld_options translate="label" module="helloworld"> <label>Hello World Config Options</label> <tab>helloconfig</tab> <frontend_type>text</frontend_type> <sort_order>1000</sort_order> <show_in_default>1</show_in_default> <show_in_website>1</show_in_website> <show_in_store>1</show_in_store> <groups> <messages translate="label"> <label>Demo Of Config Fields</label> <frontend_type>text</frontend_type> <sort_order>1</sort_order> <show_in_default>1</show_in_default> <show_in_website>1</show_in_website> <show_in_store>1</show_in_store> </messages> </groups> </helloworld_options> </sections>
6
添加配置選項:
<messages translate="label"> <label>Demo Of Config Fields</label> <frontend_type>text</frontend_type> <sort_order>1</sort_order> <show_in_default>1</show_in_default> <show_in_website>1</show_in_website> <show_in_store>1</show_in_store> <fields> <hello_message> <label>Message</label> <frontend_type>select</frontend_type> < !– adding a source model –> <source_model>helloworld/words</source_model> <sort_order>1</sort_order> <show_in_default>1</show_in_default> <show_in_website>1</show_in_website> <show_in_store>1</show_in_store> </hello_message>
</fields>
</messages>
6.1
app/code/local/Zhlmmc/Helloworld/Model/Words.phpclass Zhlmmc_Helloworld_Model_Words{ public function toOptionArray() { return array( array('value'=>1, 'label'=>Mage::helper('helloworld')->__('Hello')), array('value'=>2, 'label'=>Mage::helper('helloworld')->__('Goodbye')), array('value'=>3, 'label'=>Mage::helper('helloworld')->__('Yes')), array('value'=>4, 'label'=>Mage::helper('helloworld')->__('No')), ); }}
源模型提供了一個方法“toOptionsArray”,返回的資料時用來填充我們之前定義的配置選項的。這個方法在運行時會被“initFields”調用。“initFields”在以下類中定義
app/code/core/Mage/Adminhtml/Block/System/Config/Form.php
7
如何獲得配置資料到目前為止,我們只是講了如何設定Magento,可以讓使用者可以配置我們的模組。現在讓我們來看看如何擷取使用者的配置資料。
Mage::getStoreConfig('helloworld_options/messages/hello_message');上面這行代碼就可以擷取我們上面配置的那個“select”選項的資料。這個函數的參數是我們要擷取的資料的URI,格式如下
section_name/group_name/field_name
你也可以通過以下代碼來擷取一個組或者段的所有值
Mage::getStoreConfig('helloworld_options/messages');Mage::getStoreConfig('helloworld_options');最後,如果你想擷取針對某個特定店面(store)的資料,你可以傳入store ID
Mage::getStoreConfig('helloworld_options',1);