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
Solved! Go to Solution.
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!
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!
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.
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!
Do you know if this function is available in all Magento 2 versions?
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!