How to get config data from model in magento 2?

by Thành Trung Tô   Last Updated September 14, 2018 15:09 PM

I have code file in model.How to get config data ? I have error with AbstractMethod and getConfigData() in PHP storm .ERROR

Class AbstractMethod is deprecated

Method getConfigData is deprecated

class AdminPaymentMethod extends \Magento\Payment\Model\Method\AbstractMethod{
/**
 * Payment code
 *
 * @var string|bool
 */
const CODE                  = 'adminpaymentmethod';
protected $_code            = self::CODE;
protected $_isOffline       = true;
protected $_canUseCheckout  = false;
protected $_canUseInternal  = true;

/**
 * Get pre select option from config
 *
 * @return bool
 */
public function getDataPreSelect()
{
    return $this->getConfigData('preselect');
}}

file system.xml

<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="adminpaymentmethod" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
            <label><![CDATA[Admin Payment Method]]></label>
            <field id="active" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="0" canRestore="1">
                <label><![CDATA[Enabled]]></label>
                <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
                <comment><![CDATA[Version 1.0.0]]></comment>
            </field>
            <field id="title" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
                <label><![CDATA[Title]]></label>
            </field>
            <field id="sort_order" translate="label" type="text" sortOrder="100" showInDefault="1" showInWebsite="1" showInStore="0">
                <label><![CDATA[Sort Orderr]]></label>
                <frontend_class>validate-number</frontend_class>
            </field>
            <field id="order_status" translate="label" type="select" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="0" canRestore="1">
                <label><![CDATA[New Order Status]]></label>
                <source_model>Magento\Sales\Model\Config\Source\Order\Status\NewStatus</source_model>
            </field>
            <field id="allowspecific" translate="label" type="allowspecific" sortOrder="50" showInDefault="1" showInWebsite="1" showInStore="0" canRestore="1">
                <label><![CDATA[Payment from Applicable Countries]]></label>
                <source_model>Magento\Payment\Model\Config\Source\Allspecificcountries</source_model>
            </field>
            <field id="specificcountry" translate="label" type="multiselect" sortOrder="51" showInDefault="1" showInWebsite="1" showInStore="0">
                <label><![CDATA[Payment from Specific Countries]]></label>
                <source_model>Magento\Directory\Model\Config\Source\Country</source_model>
                <can_be_empty>1</can_be_empty>
            </field>
            <field id="preselect" translate="label" type="select" sortOrder="55" showInDefault="1" showInWebsite="1" showInStore="0" canRestore="1">
                <label><![CDATA[Pre Select]]></label>
                <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
            </field>
        </group>
    </section>
</system>

Can anyone help me ?



Answers 2


First you have to call \Magento\Framework\App\Config\ScopeConfigInterface $ScopeConfigInterface, in your constructor and then should use like below ( change 'web/secure/base_url' to which section / field value you want to get ).

LIKE :

  protected $scopeConfigInterface;

  public function __construct(

    \Magento\Framework\App\Config\ScopeConfigInterface $ScopeConfigInterface,

) {


    $this->scopeConfigInterface = $ScopeConfigInterface;
}

public function getSecureUrl() {
    return $this->scopeConfigInterface->getValue('web/secure/base_url');
}
Naveed
Naveed
September 14, 2018 14:50 PM

Please try the below code:

class AdminPaymentMethod extends \Magento\Payment\Model\Method\AbstractMethod{
    /**
     * Payment code
     *
     * @var string|bool
     */
    const CODE                  = 'adminpaymentmethod';
    protected $_code            = self::CODE;
    protected $_isOffline       = true;
    protected $_canUseCheckout  = false;
    protected $_canUseInternal  = true;
    protected $_scopeConfig;

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

    /**
     * Get pre select option from config
     *
     * @return bool
     */
    public function getDataPreSelect()
    {
        return $this->_scopeConfig->getValue('payment/adminpaymentmethod/preselect');
    }
}
Sukumar Gorai
Sukumar Gorai
September 14, 2018 14:54 PM

Related Questions




Class does not exist but mentioned Error

Updated April 17, 2018 11:09 AM

Migrate from 2.0.15 to 2.2.2

Updated January 26, 2018 05:09 AM