cancel
Showing results for 
Search instead for 
Did you mean: 

Detect the unit of measure that is being used for weight

SOLVED

Detect the unit of measure that is being used for weight

Hello,

 

Is there a function that can be called to determine what unit of measure (lbs, kg) is being used in a store?  I have a plugin that needs to determine the unit of measure for conversion purposes for an API call.

 

Thanks,

Justin

1 ACCEPTED SOLUTION

Accepted Solutions

Re: Detect the unit of measure that is being used for weight

Hi @justin_kay ,

 

You can get this value directly from store config

Try this -

You need to use the instance of \Magento\Framework\App\Config\ScopeConfigInterface in your code:

Create the method getWeightUnit()

public function getWeightUnit()
{
    return $this->_scopeConfig->getValue(
        'general/locale/weight_unit',        \Magento\Store\Model\ScopeInterface::SCOPE_STORE
    );
}

Hope it helps you.

Problem Solved! Click Kudos & Accept as Solution!

View solution in original post

5 REPLIES 5

Re: Detect the unit of measure that is being used for weight

Hi @justin_kay

 

For checking and changing the weight unit-

 

In magento admin panel -> go to store -> configuration -> general section -> locale options -> over there you will see weight unit option -> select which you want to set.

 

Magento only provide two weight unit type which is KG and LBS , but it is just number so you can customize it , if you wanted to use as a gram then.

 

Hope this helps you.

Problem Solved! Click Kudos & Accept as Solution!

Re: Detect the unit of measure that is being used for weight

Hello @Nishu Jindal 

 

I'm looking for a function to determine what the selection is programatically.  In my extension I'd like to determine which option is selected so that I can make a conversion to grams from either lbs or kg.

Re: Detect the unit of measure that is being used for weight

Hi @justin_kay ,

 

You can get this value directly from store config

Try this -

You need to use the instance of \Magento\Framework\App\Config\ScopeConfigInterface in your code:

Create the method getWeightUnit()

public function getWeightUnit()
{
    return $this->_scopeConfig->getValue(
        'general/locale/weight_unit',        \Magento\Store\Model\ScopeInterface::SCOPE_STORE
    );
}

Hope it helps you.

Problem Solved! Click Kudos & Accept as Solution!

Re: Detect the unit of measure that is being used for weight

Do you know if this function is available in all Magento 2 versions?

Re: Detect the unit of measure that is being used for weight

HI @justin_kay ,

 

You need to add /create this function to get the store config value in your plugin 

Else you can directly use this

return $this->_scopeConfig->getValue(
        'general/locale/weight_unit',
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
);

in your plugin after adding a dependency for _scopeConfig Variable.

 

Thanks!

Problem Solved! Click Kudos & Accept as Solution!