4

As I am new in Magento 2 development I am trying to add a weight unit Grams in weight drop down on product addition page on admin console but I can't get success. Can anyone please help me on this to add a new weight unit Grams?

6

Need to override \magento\vendor\magento\module-directory\Model\Config\Source\WeightUnit.php

magento\app\code\Custom\Directory\registration.php

\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Custom_Directory',
    __DIR__
);

magento\app\code\Custom\Directory\etc\module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Custom_Directory" setup_version="1.0.0">
        <sequence>
            <module name="Magento_Directory"/>
        </sequence>
    </module>
</config>

magento\app\code\Custom\Directory\etc\di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Directory\Model\Config\Source\WeightUnit" type="Custom\Directory\Model\Config\Source\WeightUnit" />
</config>

magento\app\code\Custom\Directory\Model\Config\Source\WeightUnit.php

namespace Custom\Directory\Model\Config\Source;

class WeightUnit extends \Magento\Directory\Model\Config\Source\WeightUnit
{
    /**
     * {@inheritdoc}
     */
    public function toOptionArray()
    {
        return [['value' => 'lbs', 'label' => __('lbs')], ['value' => 'kgs', 'label' => __('kgs')], ['value' => 'gms', 'label' => __('grams')]];
    }
}

Note: Be careful with your shipping methods. DHL, UPS, FedEx they allow only KG & LBS.

Reference for Magento 1 set weight units in grams

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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