Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to get the value of config variable in my custom block.php file.

Can somebody help me in this?

So far, I did following this in my block file.

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

$this->_scopeConfig->getValue('carriers/freeshipping/free_shipping_subtotal', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);

But I'm still not getting the value and showing error

( ! ) Fatal error: Uncaught Error: Call to a member function dispatch() on null in D:\wamp64\www\deboomhut0710\lib\internal\Magento\Framework\View\Element\AbstractBlock.php on line 644 ( ! ) Error: Call to a member function dispatch() on null in D:\wamp64\www\deboomhut0710\lib\internal\Magento\Framework\View\Element\AbstractBlock.php on line 644

share|improve this question

The error you're getting is because your block needs to extend at least Magento\Framework\View\Element\AbstractBlock which is not the case.

It should look like this:

namespace Vendor\Module\Block;

use Magento\Framework\View\Element\AbstractBlock;

class MyBlock extends AbstractBlock {

    public function __construct(
     \Magento\Framework\View\Element\Context $context,
     \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
     \Magento\Store\Model\StoreManagerInterface $storeManager,
     array $data = []
    )
    {
        $this->_scopeConfig = $scopeConfig;
        $this->_storeManager = $storeManager;
        parent::__construct($context, $data);
    }
}
share|improve this answer
    
Thanks for your reply. Error has gone but still not able to get the config value. I want to get the minimum amount set for free shipping at backend. – Deeps Nov 4 '16 at 12:06
    
@Deeps well your code to get the config value is totally valid. I'm not sure why it doesn't work – Raphael at Digital Pianism Nov 4 '16 at 12:16
    
@Deeps are you sure you saved the configuration for the right store ? – Raphael at Digital Pianism Nov 4 '16 at 12:17
    
Can you please post your block file here ? @Deeps – Keyur Shah Nov 4 '16 at 12:31

Please use below code :

const SUBTOTAL = 'carriers/freeshipping/free_shipping_subtotal';

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

}
public function getSubtotal() {
 $storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORE;
 return $this->scopeConfig->getValue(self::SUBTOTAL, $storeScope); //you get your value here
}

Then in your template file try to get : $this->getSubtotal();

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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