3

I have added some custom fields to the admin->configurations->sales->payment section using the following method.

created config.xml inside etc folder of my module dir.

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
<default>
    <payment>
        <ProductSpecificPayments>
            <model>Vendor\Module\Model\Carrier\ProductSpecificPayments</model>
            <name>Product Specific Payment</name>
        </ProductSpecificPayments>
    </payment>
</default>

Then created system.xml in adminhtml folder.

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
<system>
    <section id="payment">
        <group id="productspecific" translate="label" type="text" sortOrder="500" showInDefault="1" showInWebsite="1" showInStore="1">
            <label>Productspecific Payment Configuration</label>
            <field id="active" translate="label" type="select" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="0">
                <label>Enabled</label>
                <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
            </field>
            <field id="title" translate="label" type="text" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
                <label>Title</label>
            </field>
            <field id="name" translate="label" type="text" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1">
                <label>Name</label>
            </field>
            <field id="allowed_shippings" translate="label" type="multiselect" sortOrder="40" showInDefault="1" showInWebsite="1" showInStore="0">
                <label>Allowed Payment Methods</label>
                <source_model>Vendor\Module\Model\AllPayements</source_model>
            </field>
            <field id="highes_amount" translate="label" type="select" sortOrder="50" showInDefault="1" showInWebsite="1" showInStore="0">
                <label>Fallback Payment</label>
                <source_model>Vendor\Module\Model\AllPayements</source_model>
            </field>
        </group>
    </section>
</system>

now it is working fine. but How I can retrieve these saved values in product adding a page or any other files?

especialy inside a Model.

2
  • what should i use instead of SCOPE_STORES?
    – Sanjun Dev
    Commented Sep 17, 2019 at 12:04
  • You can use the SCOPE_TYPE_DEFAULT and SCOPE_STORES in the $storeScope. SCOPE_TYPE_DEFAULT for getting default view configuration while SCOPE_STORES is for getting store views settings. Check my answer for the same.
    – Sumit
    Commented Sep 17, 2019 at 12:07

3 Answers 3

3

You can get the value of the system.xml fields in your block, model, controller by adding the below code in them.

protected $_scopeConfig;

public function __construct( 
    \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
) 
{
    $this->_scopeConfig = $scopeConfig;
}

public function getYourValue() {
    $storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORES;
    return $this->_scopeConfig->getValue("section_id/group_id/field_id", $storeScope); //where section_id, group_id and field_id defined in your system.xml file
}

You can use the SCOPE_TYPE_DEFAULT and SCOPE_STORES in the $storeScope. SCOPE_TYPE_DEFAULT is for getting default view configuration setting while SCOPE_STORES is for getting store views configuration setting.

Hope it helps!!!

1

Hope below code will help you :

namespace Company\Module\Helper;

use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Store\Model\ScopeInterface;

class Data extends AbstractHelper
{

    const XML_PATH_HELLOWORLD = 'helloworld/';

    public function getConfigValue($field, $storeId = null)
    {
        return $this->scopeConfig->getValue(
            $field, ScopeInterface::SCOPE_STORE, $storeId
        );
    }

    public function getGeneralConfig($code, $storeId = null)
    {

        return $this->getConfigValue(self::XML_PATH_HELLOWORLD .'general/'. $code, $storeId);
    }

}
1

Try this one:

public function __construct(
    \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
) {
    parent::__construct($scopeConfig);
}  

And get the config value:

public function getTitle()
{
    $title = $this->getConfigData('title');
    return $title;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.