If you want to get the shipping method configuration value then you need to use the following code for Magento AbstractCarrier, as your Shipping method must be extending this class.
Magento Core has the following methods in Magento\Shipping\Model\Carrier\AbstractCarrier
/**
* Retrieve information from carrier configuration
*
* @param string $field
* @return false|string
*/
public function getConfigData($field)
{
if (empty($this->_code)) {
return false;
}
$path = 'carriers/' . $this->_code . '/' . $field;
return $this->_scopeConfig->getValue(
$path,
\Magento\Store\Model\ScopeInterface::SCOPE_STORE,
$this->getStore()
);
}
/**
* Retrieve config flag for store by field
*
* @param string $field
* @return bool
* @SuppressWarnings(PHPMD.BooleanGetMethodName)
*/
public function getConfigFlag($field)
{
if (empty($this->_code)) {
return false;
}
$path = 'carriers/' . $this->_code . '/' . $field;
return $this->_scopeConfig->isSetFlag(
$path,
\Magento\Store\Model\ScopeInterface::SCOPE_STORE,
$this->getStore()
);
}
Assume your field id is {fieldid}
then you can use the following method to get the configuration value on your FLATRATE shipping method class
/**
* get shipping method configuration on Flatrate Carrier Model
**/
$fieldidvalue=$this->getConfigData('{fieldid}');
and then you can add comparison with you static variable : flatrateConfigCustomOptionValue
if ($this->flatrateConfigCustomOptionValue === $fieldidvalue) {}
Method for Outside Shipping Method Model :
If you want to get configuration from outside of Shipping Model then you need to use the following method for that
step 1. add \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig in you constructor
step 2. Provide member variable access
step 3 : create a function to get configuration
Step 4: Get specific field id value from configuration and compare as per required
/**
*@param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
....... Other parameters................
**/
public function __construct(
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
....... Other parameters................
) {
$this->scopeConfig = $scopeConfig;
....... Other parameters................
}
public function getConfigData($field)
{
$storeID=GET/ADD YOUR STORE ID HERE;
$path = 'carriers/{youshippingmethodcode}/' . $field;
//{youshippingmethodcode} replace with your shipping method code
return $this->scopeConfig->getValue(
$path,
\Magento\Store\Model\ScopeInterface::SCOPE_STORE,
$storeID
);
}
then again use same way to get the value and do comparison
/**
* get shipping method configuration on Flatrate Carrier Model
**/
$fieldidvalue=$this->getConfigData('{fieldid}');
and then you can add comparison with you static variable : flatrateConfigCustomOptionValue
if ($this->flatrateConfigCustomOptionValue === $fieldidvalue) {}