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
2 Answers 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);
}
}
-
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– Deeps2016年11月04日 12:06:19 +00:00Commented 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 workRaphael at Digital Pianism– Raphael at Digital Pianism2016年11月04日 12:16:22 +00:00Commented Nov 4, 2016 at 12:16
-
@Deeps are you sure you saved the configuration for the right store ?Raphael at Digital Pianism– Raphael at Digital Pianism2016年11月04日 12:17:45 +00:00Commented Nov 4, 2016 at 12:17
-
Can you please post your block file here ? @DeepsKeyur Shah– Keyur Shah2016年11月04日 12:31:47 +00:00Commented Nov 4, 2016 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();