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

1

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

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.