0

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

Raphael at Digital Pianism
70.8k37 gold badges192 silver badges357 bronze badges
asked Nov 4, 2016 at 11:28

2 Answers 2

2

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);
 }
}
answered Nov 4, 2016 at 11:37
4
  • 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. Commented Nov 4, 2016 at 12:06
  • @Deeps well your code to get the config value is totally valid. I'm not sure why it doesn't work Commented Nov 4, 2016 at 12:16
  • @Deeps are you sure you saved the configuration for the right store ? Commented Nov 4, 2016 at 12:17
  • Can you please post your block file here ? @Deeps Commented Nov 4, 2016 at 12:31
0

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();

answered Nov 4, 2016 at 12:39

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.