4

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 ?

  • Can you post you full file? Also from where you want the config value, update the system.xml to your question as well? – Sukumar Gorai Sep 14 '18 at 14:47
  • i have updated my question.pls help me . – Thành Trung Tô Sep 14 '18 at 14:50
1

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');
    }
}
1

Update : This issue is already reported \Magento\Payment\Model\Method\AbstractMethod class is deprecated and You must not extend AbstractMethod class in your own payment method but just implement Magento\Payment\Model\MethodInterface interface.

You can also check Magento Devdocs that how to implement payment methods with new updates and also magento team has provided a sample payment module to use as sample

  • I know this code. I have error Class AbstractMethod is deprecated Method getConfigData is deprecated – Thành Trung Tô Sep 14 '18 at 14:53
  • Im making a payment method offline. – Thành Trung Tô Sep 14 '18 at 14:54
  • Can I replace \Magento\Payment\Model\Method\AbstractMethod with something ? – Thành Trung Tô Sep 16 '18 at 12:34
  • @ThànhTrungTô, you have to change and implement interface as detailed in devdocs. However if you still want to use that class, then you should not define any method there in and only can define / override variables. ( i.e /** protected $_code = 'adminpaymentmethod'; protected $_isOffline = false; ) – Naveed Asim Sep 16 '18 at 15:58
0

Create a function for getting configuration values in your custom module's model.

Just Use \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, In your constructor argument and set the class property: $this->scopeConfig = $scopeConfig;

$config_path = 'payment/adminpaymentmethod/preselect';
public function getDataPreSelect($config_path)
{
    return $this->scopeConfig->getValue(
        $config_path,
        \Magento\Store\Model\ScopeInterface::SCOPE_STORE
    );
}
  • Can I replace \Magento\Payment\Model\Method\AbstractMethod with something ? – Thành Trung Tô Sep 16 '18 at 12:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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