Magento – How to get the Unit of measure label of product attributes like weight,Length etc in product detail page

attributesmagento2magento2.3.0product-attributeproduct-detail-page

How to get the Unit of measure label of product attributes like weight,Length etc in product detail page?

enter image description here

Best Answer

This settings comes from system configuration.

Weight: Stores -> Configurations -> Generale -> Locale Options -> Weight Unit

use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Store\Model\ScopeInterface;

Then

/**
 * @var ScopeConfigInterface
 */
private $scopeConfig;

/**
 * @param ScopeConfigInterface $scopeConfig
 */
public function __construct(ScopeConfigInterface $scopeConfig)
{
    $this->scopeConfig = $scopeConfig;
}

Get value:

$weightUnit = $this->scopeConfig->getValue(
        'general/locale/weight_unit',
        ScopeInterface::SCOPE_STORE
    );

For dimension (width, length): here is the logic

vendor/temando/module-shipping-m2/Ui/DataProvider/Product/Form/Modifier/Dimensions.php

($weightUnit === 'kgs') ? 'cm' : 'in'
Related Topic