I am using Magento 2.1 and I am trying to get my custom configuration field's values in my custom observer.
I have tried implementing this solution, but it is not working.
I am trying using object manager in my observer:
 $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
 $qty1 = $this->objectManager->get('Magento\Framework\App\Config\ScopeConfigInterface')->getValue('membership/general/qty1');
System.xml:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
 <system>
 <tab id="beadsventure" translate="label" sortOrder="10">
 <label>beadsventure</label>
 </tab>
 <section id="membership" translate="label" sortOrder="130" showInDefault="1" showInWebsite="1" showInStore="1">
 <class>separator-top</class>
 <label>Membership Discount</label>
 <tab>beadsventure</tab>
 <resource>BeadsVenture_Membership::membership_config</resource>
 <group id="general" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="0" showInStore="0">
 <label>General Configuration</label>
 <field id="qty1" translate="label" type="text" sortOrder="0" showInDefault="1" showInWebsite="0" showInStore="0">
 <label>Quantity for Level 1</label>
 <validate>validate-number</validate>
 <comment>This will threshold Quantity for Level 1 (If empty then Default value is 3).</comment>
 </field>
 <field id="price1" translate="label" type="text" sortOrder="0" showInDefault="1" showInWebsite="0" showInStore="0">
 <label>Price for Level 1</label>
 <validate>validate-number</validate>
 <comment>This will threshold Price for Level 1 (If empty then Default value is 500ドル).</comment>
 </field>
 <field id="qty2" translate="label" type="text" sortOrder="1" showInDefault="1" showInWebsite="0" showInStore="0">
 <label>Quantity for Level 2</label>
 <validate>validate-number</validate>
 <comment>This will threshold Quantity for Level 2 (If empty then Default value is 5).</comment>
 </field>
 <field id="price2" translate="label" type="text" sortOrder="1" showInDefault="1" showInWebsite="0" showInStore="0">
 <label>Price for Level 2</label>
 <validate>validate-number</validate>
 <comment>This will threshold Price for Level 2 (If empty then Default value is 1500ドル).</comment>
 </field>
 <field id="qty3" translate="label" type="text" sortOrder="2" showInDefault="1" showInWebsite="0" showInStore="0">
 <label>Quantity for Level 3</label>
 <validate>validate-number</validate>
 <comment>This will threshold Quantity for Level 3 (If empty then Default value is 10).</comment>
 </field>
 <field id="price3" translate="label" type="text" sortOrder="2" showInDefault="1" showInWebsite="0" showInStore="0">
 <label>Price for Level 3</label>
 <validate>validate-number</validate>
 <comment>This will threshold Price for Level 3 (If empty then Default value is 3000ドル).</comment>
 </field>
 <field id="qty4" translate="label" type="text" sortOrder="3" showInDefault="1" showInWebsite="0" showInStore="0">
 <label>Quantity for Level 4</label>
 <validate>validate-number</validate>
 <comment>This will threshold Quantity for Level 4 (If empty then Default value is 20).</comment>
 </field>
 <field id="price4" translate="label" type="text" sortOrder="3" showInDefault="1" showInWebsite="0" showInStore="0">
 <label>Price for Level 4</label>
 <validate>validate-number</validate>
 <comment>This will threshold Price for Level 4 (If empty then Default value is 5000ドル).</comment>
 </field>
 </group>
 </section>
 </system>
</config>
Observer:
namespace BeadsVenture\Membership\Observer;
 class Beadsmemberamount implements \Magento\Framework\Event\ObserverInterface
 { 
 public function decide_group_id ($amount, $id, $qty)
 {
 $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
 $price1 = $objectManager->get('Magento\Framework\App\Config\ScopeConfigInterface')->getValue('membership/general/price1');
 if($amount >= $price1) {
 $id = 4;
 }
 return $id;
 }
 }
Please Help!
- 
 What problem your have facingAmit Bera– Amit Bera ♦2018年05月03日 11:49:28 +00:00Commented May 3, 2018 at 11:49
- 
 Please share the system.xml code and also it observer codeAmit Bera– Amit Bera ♦2018年05月03日 11:49:51 +00:00Commented May 3, 2018 at 11:49
- 
 @AmitBera Updated the detailsWasiq Shahrukh– Wasiq Shahrukh2018年05月03日 11:59:00 +00:00Commented May 3, 2018 at 11:59
- 
 code seems code. Are sure that you observer is running?Amit Bera– Amit Bera ♦2018年05月03日 12:10:50 +00:00Commented May 3, 2018 at 12:10
- 
 Yes it is running, I haven't mentioned my "execute" methodWasiq Shahrukh– Wasiq Shahrukh2018年05月03日 12:27:36 +00:00Commented May 3, 2018 at 12:27
1 Answer 1
Add function in observer file:
public function __construct(\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig)
 {
 $this->_scopeConfig = $scopeConfig;
 }
and update your code:
 public function decide_group_id ($amount, $id, $qty)
 {
 $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
 $price1 = $objectManager->get('Magento\Framework\App\Config\ScopeConfigInterface')->getValue('membership/general/price1');
 if($amount >= $price1) {
 $id = 4;
 }
 return $id;
 }
with
 public function decide_group_id ($amount, $id, $qty)
 {
 $price1 = $this->_scopeConfig->getValue('membership/general/price1', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
 if($amount >= $price1) {
 $id = 4;
 }
 return $id;
 }
After that clear cache and run command php bin/magento setup:di:compile.
Explore related questions
See similar questions with these tags.