如何添加 Magento 后台的Configuration配置

在magento 后台添加Configuration配置

我以设置是否发送email的控制功能为例说明操作步骤:

  1. 创建 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>
  1. 创建配置文件 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>
  1. 创建获取配置项的值的公共方法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
    );
}
  1. 在你的代码里调用配置项,完成你的业务逻辑
  • 5
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值