I’m pretty new to Magento 2, so most probably I’m missing something simple here.
I’ve defined a new module, and inside its etc/frontend
folder I created a config.xml file with this content:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
<default>
<example>
<values>
<shipping_method>fedex</shipping_method>
<payment_method>paypal</payment_method>
</values>
</example>
</default>
</config>
I’ve also created an Action class with this content:
class Index extends MagentoFrameworkAppActionAction
{
/** @var RawFactory */
private $rawFactory;
/** @var ScopeConfigInterface */
private $scopeConfig;
public function __construct(
Context $context,
RawFactory $rawFactory,
ScopeConfigInterface $scopeConfig
) {
$this->rawFactory = $rawFactory;
$this->scopeConfig = $scopeConfig;
parent::__construct($context);
}
public function execute()
{
$shippingMethod = $this->scopeConfig->getValue('example/values/shipping_method');
$paymentMethod = $this->scopeConfig->getValue('example/values/payment_method');
$output = $this->rawFactory->create();
$output->setContents(implode("<br/>", [
'Shipping method: ' . $shippingMethod,
'Payment method: ' . $paymentMethod
]));
return $output;
}
}
The issue is $shippingMethod
and $paymentMethod
are both null, unless I move the config.xml file to etc
root folder. I’ve run this on that controller method:
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$objectManager->get('MagentoFrameworkAppState')->getAreaCode();
And the result is frontend.
What am I missing? I should be able to get the values from config.xml when it’s located in the frontend area, should I? I’ve disabled the cache, cleared opcache anytime I made a change, run magento setup:di:compile, and it always return null for the configuration values when config.xml is inside etc/frontend
.