在magento 后台添加Configuration配置
我以设置是否发送email的控制功能为例说明操作步骤:
- 创建 system.xml文件。如:/app/code/模块名/etc/adminhtml/system.xml
<group id="general" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="9" translate="label">
<label>General</label>
<field id="send_email" translate="label" type="select" sortOrder="25" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Enable Sending Email</label>
<comment>This option will enable/disable sending emails when the payment status updated</comment>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
</group>
- 创建配置文件 config.xml,设置默认值。 例如:/app/code/模块名/etc/config.xml
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
<default>
<email_setting>
<general>
<send_email>0</send_invoice_email>
</general>
</email_setting>
</default>
</config>
- 创建获取配置项的值的公共方法Data.php 例如:/app/code/模块名/Helper/Data.php
public function isEnablesSendInvoiceEmail()
{
return (bool)$this->scopeConfig->getValue(
'email_setting/general/send_email',
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
);
}
- 在你的代码里调用配置项,完成你的业务逻辑