2

I will be injecting this into a ViewModel or Plugin. I have a custom configurable field in the Admin->configuration->Sales->Deliver Methods->flatrate and I need to check the value of it to compare with another string because it will change over time.

Example:

if ($this->flatrateConfigCustomOptionValue === $value) {}

2 Answers 2

1

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) {} 
1

Hi all the admin configuration are store in core_config_data. You can get the store config data using scopeconfig. How to get value from `core_config_data` table in Magento 2

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.